2017-05-05 104 views
0

當我運行下面的程序,我得到在第20行的錯誤,這是我的代碼:Java的ArrayIndexOutOfBoundsException異常錯誤

package J1; 
import java.util.Scanner; 

public class SpeedLimit { 

public static void main(String[] args) { 
    Scanner keyboard = new Scanner(System.in); 
    int input = keyboard.nextInt(); 

    String[] tab = new String[2]; 
    String output=""; 
    int speed = 0; 
    while(input!=-1){ 
     int last =0; 
     for (int i=0; i<input ; i++){ 

      String pair = keyboard.next(); 

      tab = pair.split(" "); 
      speed = speed + Integer.parseInt(tab[0])*(Integer.parseInt(tab[1])-last); 
      last = Integer.parseInt(tab[1]); 
     } 
     output = output +speed + "miles" + "\n"; 
     speed =0; 
    input = Integer.parseInt(keyboard.nextLine()); 
    } 
    System.out.println(output); 
} 

} 

當我運行的代碼,我從鍵盤輸入下列輸入:

3 
20 2 
30 6 
10 7 
2 
60 1 
30 5 
4 
15 1 
25 2 
30 3 
10 5 
-1 

得到這個結果作爲輸出: 170英里 180英里 90英里

但我GE T,則下面錯誤,當我運行的代碼

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 
at J1.SpeedLimit.main(SpeedLimit.java:20) 
+1

'字符串對= keyboard.nextLine();' –

+0

我不理解 –

+0

在'String pair = keyboard.next();'.next()'返回「這個掃描器的下一個完整標記。」我認爲@SanketMakani建議它可能只返回第一個單詞而不是整行。如果你用建議的替換這條線,它將獲取整條線。 – dolan

回答

2

String pair = keyboard.next();這隻能讀取一個令牌,該令牌是由" "所以當你通過" "分裂pair分離。它只會有一個元素,即字符串本身。所以你需要閱讀整行,然後用分隔的" "分隔它。

另一個錯誤是,當您使用String pair = keyboard.nextLine();更改該行時,由於系統認爲Enter鍵作爲.nextLine()方法的輸入,您仍會收到錯誤。所以你需要放棄額外的不必要的輸入。

while(input!=-1){ 
    int last =0; 
    for (int i=0; i<input ; i++){ 

     int ip1=keyboard.nextInt(); 
     int ip2=keyboard.nextInt(); 

     speed = speed + ip1*(ip2-last); 
     last = ip2; 
    } 
    output = output +speed + "miles" + "\n"; 
    speed =0; 
input = keyboard.nextInt(); 
} 
+0

當我在執行'4'中輸入4並且在J1.SpeedLimit.main(SpeedLimit.java:21)給我這個錯誤'Exception in thread'main「java.lang.ArrayIndexOutOfBoundsException:1 \t時, ' –

+0

完美的作品在我的機器上。我會更新代碼嘗試該代碼! –

+0

@MedAda代碼已更新。嘗試這個! –

0

您正在閱讀的變量對走錯了路,然後你把它分解並分配給標籤從而未能自動抓取索引原因對變量有一個問題。

* nextLine():讀取當前行的其餘部分,即使它是空的。

 keyboard.nextLine(); //To avoid the exception you commented 
     String pair = keyboard.nextLine(); //here is solved 
     tab = pair.split(" "); 
+0

但是當我嘗試nextLine()而不是next()時,我得到了這個錯誤: '線程中的異常「main」java.lang.NumberFormatException:對於輸入字符串:「」at java.lang.NumberFormatException.forInputString(Unknown源) \t at java.lang.Integer。parseInt(Unknown Source) \t at java.lang.Integer.parseInt(Unknown Source) \t at J1.SpeedLimit.main(SpeedLimit.java:20)' –

+0

你應該做keyboard.nextLine();當你開始循環吃掉輸入鍵。 –

0

Keyboard.next()將只讀取輸入直到空間,因此配對和陣列將具有隻有一個號碼,因此標籤[1]結果arrayOutOfBound異常。使用nextLine()方法以空格讀取輸入。

0

你可以嘗試在你的代碼更改如下:

public static void main(String[] args) { 
    Scanner keyboard = new Scanner(System.in); 
    int input = Integer.parseInt(keyboard.nextLine()); 

    String[] tab = new String[2]; 
    String output=""; 
    int speed = 0; 
    while(input!=-1){ 
     int last =0;    
     for (int i=0; i<input ; i++){ 
      String pair = keyboard.nextLine();    
      tab = pair.split(" "); 
      speed = speed + Integer.parseInt(tab[0].trim())*(Integer.parseInt(tab[1].trim())-last); 
      last = Integer.parseInt(tab[1]); 
     } 
     output = output +speed + " miles " + "\n"; 
     speed =0; 
    input = Integer.parseInt(keyboard.nextLine()); 
    } 
    System.out.println(output); 
} 
0

我did'n真正瞭解你是如何提供的輸入。但是,如果「3」碰巧是你的第一行,那麼split(「」)將返回一個長度爲1的數組。因此,tab [0]將返回3,tab [1]會給你一個nullPointerException。

嘗試執行你的管線20

之前加入對標籤的長度的檢查這應該做的伎倆:

if(tab.length() > 1){ 

    speed = speed + Integer.parseInt(tab[0])*(Integer.parseInt(tab[1])-last); 

} 
+0

我想EXCUTE這一行: '速度=速度+的Integer.parseInt(標籤[0])*(的Integer.parseInt(製表符[1]) - 最後);'我不想跳過它 –

相關問題