In Java, how, if possible, can I convert numerical scanner input (such as 2 or 87) into an integer variable? What I'm using now yields the error message:
Exception in thread "main" java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.valueOf(Unknown Source) at diDecryption.Didecryption.main(Didecryption.java:226) And this is the code I'm using to do it (pieced together, it's part of a much larger program):
System.out.println("Enter first number"); Scanner sc=new Scanner(System.in); String name=sc.next(); int result = Integer.valueOf(name); if (result / 2 == 1){ System.out.println("a"); The purpose of the program is to decode an encrypted message. The input is numerical, and if I remove the string to int converter, the division does not work. How do I fix this?
null)null. They will throw an exception if there's nothing to read. Please provide a minimal reproducible example.