2017-04-09 27 views
-1
I have read file using scala and for the missing values i have to print as "Missing". I have used case/match/Some/Option to handle it. I end up with "IndexOutOfBound" exception. I have used try catch as well in the code but no luck. Any help will be appreciated ?? 

我正在讀取一個文件,並且該文件有一些缺失的值,我必須用值更新爲「MISSING」。處理在文件中使用scala丟失值

package HW9 

    object WeatherStub { 

//分配文件名
VAL文件名= 「weather.csv」

 def main(args: Array[String]): Unit = { 

//調用讀天氣方法 readWeather(文件名)

 } 

    //Method to handle missing and exception 
     def readWeather(fn: String): Unit = { 
     var weatherMuteMap = scala.collection.mutable.Map[String, String]() 
     def IsEmptyOrNull(s:String): Option[String] = {try {Some(s.toString)} catch {case _ => None}} 

    //Reading files 
     for(line <- io.Source.fromFile(fn).getLines()){ 
      val list1 = line.split(",").map(_.trim).toList 

    //Handling missing values 
      val TotalPrecp = IsEmptyOrNull(list1(1).toString) match { case Some(i) => i case _ => "Missing"} 
      val LowPrecp = IsEmptyOrNull(list1(2).toString) match { case Some(i) => i case _ => "Missing" } 
      val HighPrecp = IsEmptyOrNull(list1(3).toString) match { case Some(i) => i case _ => "Missing" } 

//Concatenating values to a map 
      weatherMuteMap(list1(0)) = TotalPrecp + LowPrecp + HighPrecp 
//Print  
      println(weatherMuteMap) 

     } 
     } 

     } 

示例數據來自檔案: -

2016-01-01,0,-13.28,-1.11 
    2016-01-02,0,-10,0 
    2016-01-03,0,-10,0 
    2016-01-04,0,-12.78,-2.22 
    2016-01-06,0,-6.11,0.61 
    2016-01-07,0.05,-0.61,1 
    2016-01-08,0.1,,1 
    2016-01-09,0.13,-5.61,0 
    2016-01-21,0,, 
    2016-01-22,0,, 
    2016-01-23,,-9.39,-6.11 
    2016-02-19,0,, 
    2016-02-20,0,0,0 
    2016-02-21,,, 
    2016-02-22,0,-0.61,0.61 
    2016-02-23,,, 

    Error:- 

    Exception in thread "main" java.lang.IndexOutOfBoundsException: 2 
     at scala.collection.LinearSeqOptimized$class.apply(LinearSeqOptimized.scala:65) 
     at scala.collection.immutable.List.apply(List.scala:84) 
     at HW9.WeatherStub$$anonfun$readWeather$1.apply(WeatherStub.scala:19) 
     at HW9.WeatherStub$$anonfun$readWeather$1.apply(WeatherStub.scala:16) 
     at scala.collection.Iterator$class.foreach(Iterator.scala:893) 
     at scala.collection.AbstractIterator.foreach(Iterator.scala:1336) 
     at HW9.WeatherStub$.readWeather(WeatherStub.scala:16) 
     at HW9.WeatherStub$.main(WeatherStub.scala:8) 
     at HW9.WeatherStub.main(WeatherStub.scala) 

    Process finished with exit code 1 
+2

可以格式化你的問題是否正確? – jamborta

回答

1

如果您運行的一些數據的例子:

scala> val str = "2016-02-23,,," 
scala> str.split(",").map(_.trim).toList 
res0: List[String] = List(2016-02-23) 

你可以看到,你只得到第一個值列表,因此IndexOutOfBoundsException異常錯誤。您可以關閉此行爲是這樣的:

scala> str.split(",", -1).map(_.trim).toList 
res1: List[String] = List(2016-02-23, "", "", "") 

看一看這個線程解釋了原因:Java String split removed empty values

編輯:

有在你的代碼兩件事情,沒有意義,IsEmptyOrNull的定義需要String,因此try塊永遠不會失敗,因此它永遠不會返回無。另外line.split總是會返回Array[String],所以元素已經是String。你能想到的,遺漏值是如何空,並在數據表示,像這樣的東西替換IsEmptyOrNull

def IsEmptyOrNull(s: String): Option[String] = { 
    s match { 
    case "" => None 
    case _ => Some(s) 
    } 
} 
+0

這是一個很好的結果。謝謝!!。我能夠修復入站錯誤異常,但仍然沒有打印「丟失」值。不知道爲什麼? – Issaq