Java 如何解决numberformatexception和获取输入字符串&引用;?

Java 如何解决numberformatexception和获取输入字符串&引用;?,java,exception,numberformatexception,Java,Exception,Numberformatexception,我试图制作一个程序,其中输入由几行组成,第一行表示字符序列的后续行数。这些字符序列将被“”拆分。将拆分后的字符转换为整数,得到其和 For Example: 2 2 3 4 5 2 3 Sum is 5 4 5 Sum is 9 现在,当我输入相同的输入时,我得到这个 2 2 3 4 5 Sum is 0 2 3 Sum is 5 4 5 Sum is 9 java.lang.Number

我试图制作一个程序,其中输入由几行组成,第一行表示字符序列的后续行数。这些字符序列将被“”拆分。将拆分后的字符转换为整数,得到其和

  For Example:      
  2      
  2 3      
  4 5      

  2 3 Sum is 5
  4 5 Sum is 9
现在,当我输入相同的输入时,我得到这个

  2
  2 3
  4 5
   Sum is 0
  2 3 Sum is 5
  4 5 Sum is 9

  java.lang.NumberFormatException: For input string: ""
 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at ITweek.sum2(ITweek.java:33)
at ITweek.main(ITweek.java:18)
为什么我有一个总和为0的回报?以及NumberFormatException错误:对于输入字符串:“”?我不明白。我不知道如何以及在哪里找到答案。任何帮助都将不胜感激。多谢各位

这是我的密码

  import java.util.ArrayList;
  import java.util.List;
  import java.util.Scanner;

  public class Prob {
      public static void main(String args[]){

    Scanner input = new Scanner(System.in);
    int loop = input.nextInt();

    String numberString[]=new String[loop+1];

    for(int i=0; i<=loop; i++){
        String ans = input.nextLine();
        numberString[i] = ans;
    }
    sum2(numberString);

}

  static void sum2(String []answers){
  for(String b : answers){
         System.out.print(""+b+" ");

         String splittedNumber[] = b.split(" ");

         int sum = 0;
         for(String j : splittedNumber){
             try{
                 sum=sum+Integer.parseInt(j);
             }catch(Exception x){
                 x.printStackTrace();
         }
         }
            System.out.println("Sum is "+sum);
     }
   }
  }
import java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
公共类问题{
公共静态void main(字符串参数[]){
扫描仪输入=新扫描仪(System.in);
int loop=input.nextInt();
字符串编号字符串[]=新字符串[循环+1];

对于(inti=0;i这里有一个提示,请看这一行:stringnumberstring[]=newstring[loop+1]; 您初始化的数组大小是多少?您实际放入了多少项?

很明显,您的问题是;),它在代码的这一部分

    Scanner input = new Scanner(System.in);
    int loop = input.nextInt();

    String numberString[]=new String[loop]; //remove last block of array. +1 is useless

    for(int i=0; i<=loop; i++){
        String ans = input.nextLine();
        numberString[i] = ans;
    }
    sum2(numberString);

}
扫描仪输入=新扫描仪(System.in);
int loop=input.nextInt();
String numberString[]=新字符串[loop];//删除数组的最后一块。+1无效

对于(int i=0;i而言,
main
方法中存在一些需要修复的错误

Scanner input = new Scanner(System.in);
//create a new scanner to read string as input doesn't advance to next line after  reading int
Scanner inputString = new Scanner(System.in);
    int loop = input.nextInt();
//loop through to enter your input and process with method call.
    for (int i = 0; i < loop; i++) {
//you just need one element array to store input string which is separated by whitespace
        String numberString[] = new String[1];
        numberString[0] = inputString.nextLine();
        sum2(numberString);
    }
扫描仪输入=新扫描仪(System.in);
//创建一个新的扫描器来读取字符串,因为输入在读取int后不会前进到下一行
扫描仪输入字符串=新扫描仪(System.in);
int loop=input.nextInt();
//通过循环输入输入并使用方法调用进行处理。
for(int i=0;i

发生NFE是因为试图将空字符串转换为整数。我希望这会有所帮助。

我在其上添加了1,因为如果我没有,则会出现ArrayIndexOutOfBoundsException。啊,这是因为for循环中的另一个问题。听起来像是学校作业,所以我认为如果我给您的提示不止一个,您的讲师不会高兴:)如果有一个“”,不要将其解析为整数。一个适当的If-else语句就足够了。啊,好的,我明白了。我的一个疏忽。谢谢。=)哦,我明白了。谢谢你的integer.parseInt(input.readLine());并将for循环的条件更改为iI,从for块中删除了+1和=条件。否则,您将在for循环的最后一次迭代中再次结束对空字符串的求和。欢迎使用。
public static void main(String args[]){

Scanner input = new Scanner(System.in);
int loop = input.readInt());
input.readLine;
String numberString[]=new String[loop]; //remove last block of array. +1 is useless

for(int i=0; i<loop; i++){
    String ans = input.nextLine();
    numberString[i] = ans;
}
sum2(numberString);
Scanner input = new Scanner(System.in);
//create a new scanner to read string as input doesn't advance to next line after  reading int
Scanner inputString = new Scanner(System.in);
    int loop = input.nextInt();
//loop through to enter your input and process with method call.
    for (int i = 0; i < loop; i++) {
//you just need one element array to store input string which is separated by whitespace
        String numberString[] = new String[1];
        numberString[0] = inputString.nextLine();
        sum2(numberString);
    }