2017-05-05 103 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]) - 最后);'我不想跳过它 –

相关问题