0

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?

4
  • Your code works fine for me if I give it a number. What, exactly, are you providing as input on system in? (The error message you've posted indicates you're trying to parse null) Commented Feb 22, 2017 at 15:14
  • What is your input ? The code looks fine to me. ideone.com/omNdEF Commented Feb 22, 2017 at 15:18
  • 2
    I doubt that your exception message matches your code. Scanner methods don't return null. They will throw an exception if there's nothing to read. Please provide a minimal reproducible example. Commented Feb 22, 2017 at 15:33
  • The steps given do not reproduce the quoted problem. Commented Aug 31, 2018 at 20:31

5 Answers 5

2

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(); } } 
Sign up to request clarification or add additional context in comments.

Comments

1

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 

Comments

0
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

1 Comment

That won't make any difference, Java's quite happy to unbox the Integer returned by valueOf().
0

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"); } } 

Comments

0

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"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.