Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: NumberFormatException

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NumberFormatException

    Iím doing this exercise where I need to design a program that will generate a random number between 1 and 100 and then let the user guess the number and tell them whether the guess is too high or too low. Hereís the code:

    The Evaluator class:

    import java.io.*;
     
    public class Evaluator
    {
        private KeyboardReader reader = new KeyboardReader();
     
    	public String evaluate(int guess, int secretNum)
    	{   
    	    if(guess > secretNum)
    		   return "Too high";
    		else 
    		   return "Too low";
    	} // evaluate()
     
    	public void run()
    	{
    	    int secretNum = 1 + (int)(Math.random()*100);
    		reader.prompt("What's your guess? -> ");
    		guess = reader.getKbInt();
    		reader.display(evaluate(guess, secretNum));
    	} // run()
     
    	public static void main(String args[])
    	{   
    	    Evaluator app = new Evaluator();
    		app.run();
    	} // main
    }

    The KeyboardReader class

    import java.io.*;
     
    public class KeyboardReader
    {
     
    	private BufferedReader reader;
     
    	public KeyboardReader()
    	{
    	    reader = new BufferedReader(new InputStreamReader(System.in));
        }  // KeyboardReader
     
        private String readKeyboard()
    	{
    	    String line = "";
    		try
    		{   reader.readLine();   }
    		catch (IOException e)
    		{   e.printStackTrace(); }
    		return line; 
    	} // readKeyboard()
     
    	public String getKbInput()
    	{   return readKeyboard();
    	}
     
    	public int getKbInt()
    	{   return Integer.parseInt(readKeyboard());
    	}
     
    	public double getKbDouble()
    	{   return Double.parseDouble(readKeyboard());
    	}
     
    	public void prompt(String s)
    	{   System.out.print(s); 
    	}
     
    	public void display(String s)
    	{   System.out.print(s);
    	}
    } // KeyboardReader

    I entered an integer and a NumberFormatException was thrown:

    C:\LearnJava>java Evaluator
    What's your guess? -> 4
    Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.base/java.lang.NumberFormatException.forInputString(Num berFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:668)
    at java.base/java.lang.Integer.parseInt(Integer.java:776)
    at KeyboardReader.getKbInt(KeyboardReader.java:28)
    at Evaluator.run(Evaluator.java:21)
    at Evaluator.main(Evaluator.java:28)

    How do I fix this?
    Last edited by Bao; July 17th, 2019 at 09:39 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,682
    Thanks
    53
    Thanked 2,421 Times in 2,374 Posts

    Default Re: NumberFormatException

    Here’s the code:
    Please copy and paste here the code wrapped in code tags.
    a NumberFormatException was thrown:
    Also copy the full text of the error message and paste it here.

    Don't post images. Text can not be copied from an image to include in the response.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NumberFormatException

    Sorry, I updated it

    --- Update ---

    I figured it out guys, I forgot to assign the "line" variable to the "reader.readLine()" method.

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    263
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: NumberFormatException

    Great
    Whatever you are, be a good one

Similar Threads

  1. Java.lang.NumberFormatException thrown
    By Tiash in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 29th, 2014, 11:04 AM
  2. JOptionPane NumberFormatException,
    By jmrink in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 13th, 2013, 11:18 AM
  3. numberformatexception
    By takamine in forum Exceptions
    Replies: 2
    Last Post: February 21st, 2013, 04:50 PM
  4. java.lang.numberformatexception error
    By natalie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 19th, 2010, 04:16 AM

Tags for this Thread