Problems with user inputs in java

Deadly 提交于 2020-05-14 08:44:19

问题


I am new to java and I am still getting used to most of the syntax. Here I tried to get two inputs (the first one being an int and the second one a string) and later one I do some calculations with them. But when I run the program, it gives this message:

Exception in thread "main" java.lan._NumberFormatException: For input string: "" 
at java.lang.NumberFormatException.forInputString(NuthberFormatException.java:65) 
at java.lang.Integer.parseInt(Integer.java:592) 
at java.lang.Integer.parseInt(Integer.java:615) 
at Main.main(Main.java:13) 

.

import java.util.Scanner;  
import java.util.Arrays;
public class Main {
   public static void main(String args[]) {
    Scanner myObj = new Scanner(System.in);  
    int N = myObj.nextInt();
    String Input = myObj.nextLine();
    String[] Inputs = Input.split(" ", 0);
    int size = Inputs.length;
      int [] a = new int [size];
      for(int i=0; i<size; i++) {
         a[i] = Integer.parseInt(Inputs[i]);
      }
  }
}

My understanding is that the program is assuming both inputs as the same. But how do I fix that?


回答1:


You should address the following problems with your code:

  1. Though not essential from the execution point-of-view, you should always display a message for the input (an example is given below); otherwise, the user remains clueless.
System.out.println("Enter a few integers separated by a space: ");
String Input = myObj.nextLine();
  1. Though not essential from the execution point-of-view, you should always follow Java naming conventions e.g. int N should be written as int n and String[] Inputs should be String[] inputs as per the naming conventions.
  2. Since you are not doing anything with N, remove the line, int N = myObj.nextInt();.
  3. Even if you remove the line, int N = myObj.nextInt();, you may face NumberFormatException for inputs like 10 20 30 where there are more than one spaces. You should split on "\\s+" instead of " " which takes into account only one space. Note that "\\s+" will take care of one or more spaces.
  4. Finally, even though you split on "\\s+", you may face NumberFormatException for inputs like 10 x 30 which has a non-integer entry e.g. x which can not be parsed into an integer. To deal with such cases, you need to handle NumberFormatException as given below:
// ...
for (int i = 0; i < size; i++) {
    try {
        a[i] = Integer.parseInt(Inputs[i]);
        // ...
    } catch (NumberFormatException e) {
        System.out.println("Ignoring " + Inputs[i] + " as it is not an integer.");
        a[i] = Integer.MIN_VALUE;
    }
    // ...
}
// ...

Note that a[i] = Integer.MIN_VALUE; sets Integer.MIN_VALUE into the array for the invalid entries. You can put some other value of simply ignore it as per your requirement. If you ignore it, that index will have the default value of int which is 0.

Feel free to comment in case of any doubt/issue.



来源:https://stackoverflow.com/questions/61156113/problems-with-user-inputs-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!