2013-07-22 18 views
-1

我使用這個方法來獲取對Spotify的zipfsong拼圖輸入:https://www.spotify.com/us/jobs/tech/zipfsong/這是得到這個難題的輸入的正確方法嗎?

public static ArrayList<ArrayList<String>> fileReader() throws IOException { 
    Scanner scanner = new Scanner(System.in);   
    ArrayList<ArrayList<String>> fileContents = new ArrayList<ArrayList<String>>(); 

    try{      
     String currentLine = scanner.nextLine();     
     ArrayList<String> fileRow = new ArrayList(Arrays.asList(currentLine.split(" "))); 
     fileContents.add(fileRow); 
     int numberOfInputLines = Integer.parseInt(fileRow.get(0)); 
     int numberOfOutputLines = Integer.parseInt(fileRow.get(1)); 
     if (numberOfInputLines < numberOfOutputLines) { 
      return null; 
     } 

     for (int lineCount = 0; lineCount < numberOfInputLines; lineCount++) { 
      currentLine = scanner.nextLine();    
      fileRow = new ArrayList(Arrays.asList(currentLine.split(" "))); 
      fileContents.add(fileRow);       
     }    
    } catch(Exception e) { 
     System.out.println("Read Error");  
     return null; 
    } finally { 
     scanner.close(); 
    }  
    return fileContents; 
} 

所以,我基本上都看過的第一線,發現行數來,然後用掃描儀讀取。現在,由於某種原因,我一直在獲取ArrayIndexOutOfBoundsException。是因爲這個輸入法嗎?

+0

什麼是異常的完整堆棧跟蹤? –

+1

當我運行它時,我不明白。但是當我提交它時,我只是得到ArrayIndexOutOfBoundsException。而已。 –

+0

我相信我也問過你最後一個問題中的堆棧跟蹤(這幾乎完全像這個)。它有助於縮小問題範圍。 –

回答

0

既然你沒有一個堆棧跟蹤,這是一個有點難以縮小的問題,但可以從該行得到您的錯誤:

int numberOfOutputLines = Integer.parseInt(fileRow.get(1)); 

這將返回一個錯誤,如果

ArrayList<String> fileRow = new ArrayList(Arrays.asList(currentLine.split(" "))); 

只會導致一個字符串。嘗試分離,在使用Arrays.asList之前先將分割線分配給數組。

由於您沒有堆棧跟蹤,因此這一切都很模糊。

相關問題