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?
System.out.println("Enter first number");
Scanner sc=new Scanner(System.in);
String name=sc.next();
int result = Integer.parseInt(name);
if (result / 2 == 1){
System.out.println("a");
parseint changes it to a primitive int rather than an Integer object
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?
In your stacktrace you have null
as parameter in Integer.valueOf(name)
. Seems your console produce some invalid input sequence. Try to check it with sc.hasNext()
condition:
System.out.println("Enter first number");
Scanner sc = new Scanner(System.in);
if (sc.hasNext()) {
String name = sc.next();
int result = Integer.parseInt(name);
if (result / 2 == 1) {
System.out.println("a");
}
}
Try
System.out.println("Enter first number");
Scanner sc=new Scanner(System.in);
int name=sc.nextInt();
if ((name / 2) == 1)
System.out.println("a");
RUN
run:
Enter first number
2
a
Try this code
package exmaple;
import java.util.Scanner;
public class Parser {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name = in.next();
try{
int result = Integer.parseInt(name);
if(result / 2 == 1) {
System.out.println("a");
}
} catch(Exception exception) {
}
in.close();
}
}
try
System.out.println("Enter first number");
Scanner sc=new Scanner(System.in);
int result = sc.nextInt;
if (result / 2 == 1){
System.out.println("a");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.