Java没有';t返回假定的函数值
下面的代码是我程序的一部分,不是全部。在lasTal()中,我允许用户按数字顺序输入数字。一旦用户完成,他应该按enter键,以便我的程序对输入的空字符串作出反应并返回数字列表Java没有';t返回假定的函数值,java,Java,下面的代码是我程序的一部分,不是全部。在lasTal()中,我允许用户按数字顺序输入数字。一旦用户完成,他应该按enter键,以便我的程序对输入的空字符串作出反应并返回数字列表 public class Nastaord{ public static int bFinal, cFinal; public static ArrayList<Integer> tallistaFinal; private static ArrayList<Integer&g
public class Nastaord{
public static int bFinal, cFinal;
public static ArrayList<Integer> tallistaFinal;
private static ArrayList<Integer> lasTal(){
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> tallista = new ArrayList<Integer>(); //Det vi ska ha talföljden i
int i = 0; //räknare för tallista
while(true){
System.out.print("Enter number, or press enter if you are done: ");
String input = scanner.nextLine();
if(input == ""){
return tallista;}
int nytt_tal = Integer.parseInt(input);
tallista.add(nytt_tal);
i++;
}
在我看来,程序从未为空字符串输入if语句,而是尝试将字符串“”转换为int,这是不可能的。这是为什么?我可以做些什么来修复它?尝试“”。等于(输入)
而不是输入==“”
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at Nastaord.lasTal(Nastaord.java:18)
at Nastaord.main(Nastaord.java:45)