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 12 of 12

Thread: Exception in thread "main"?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main"?

    Hello, I recently decided to use the skills I acquired so far to build a basic calculator (very basic). But for some reason I get this exception when I run the program.

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at CalculatorBasic.main(CalculatorBasic.java:9)

    This is the code:

    import java.util.Scanner;
     
     
    class CalculatorBasic {
    	public static void main(String args[]) {
    		Scanner myscanner = new Scanner(System.in);
    		double fnum, second, answer;
    		System.out.println("Enter the first number: ");
    		fnum = myscanner.nextDouble();
    		System.out.println("Enter the second number: ");
    		second = myscanner.nextDouble();
    		answer = fnum + second;
    		System.out.println(answer);
     
    	}
    }

    I would like to know what's wrong with this code, thank you on advance.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Exception in thread "main"?

    When the user enters a number, they press enter. So you have a double and the enter character "in" the Scanner.

    Then you call the nextDouble() function, which scans past the double. But you still have the enter character in the Scanner.

    You then call nextDouble() again, which is looking for another double, but what it sees is that enter character. The enter character is not a double, so it throws that exception.

    Use the nextLine() function to scan past the enter character.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main"?

    Can you show me where should I place the .nextLine() please?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Exception in thread "main"?

    Quote Originally Posted by Empower View Post
    Can you show me where should I place the .nextLine() please?
    Where have you tried placing it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main"?

    I tried placing it under fnum = myscanner.nextDouble().

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Exception in thread "main"?

    Quote Originally Posted by Empower View Post
    I tried placing it under fnum = myscanner.nextDouble().
    And what happened when you did that? Can you post your updated code, along with the full text of your input and error?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main"?

    First, I typed fnum = myscanner.nextLine() under the first one (fnum = myscanner.nextDouble()) but it really sounded stupid because I already defined fnum as a double. So I did something crazy and placed "myscanner.nextLine()" alone under the first one.

    import java.util.Scanner;
     
     
    class CalculatorBasic {
    	public static void main(String args[]) {
    		Scanner myscanner = new Scanner(System.in);
    		double fnum, second, answer;
    		System.out.println("Enter the first number: ");
    		fnum = myscanner.nextDouble();
                    myscanner.nextLine();
    		System.out.println("Enter the second number: ");
    		second = myscanner.nextDouble();
    		answer = fnum + second;
    		System.out.println(answer);
     
    	}
    }

    The same error:

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at CalculatorBasic.main(CalculatorBasic.java:10)

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Exception in thread "main"?

    The code you just posted works fine for me. Can you post the exact input you're using?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Jan 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main"?

    What do you mean by input? If you mean code, that's exactly the one I'm using. Sorry if I misunderstood.

    --- Update ---

    I apologize - it was a failure at my end. Thank you for the nextLine() suggestion!

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Exception in thread "main"?

    I mean, what are you typing into the command line to give input to the Scanner? When the Scanner asks for a number, what are you giving it?

    Are you sure this is the code you're running?

    Edit- out of curiosity, what was the failure on your end?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member
    Join Date
    Jan 2014
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main"?

    I realized that it works even without the nextLine(). So I believe it's all good now.

    My failure: Used a number that is not a double.

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Exception in thread "main"?

    Ah I see that now. I was thinking of the following situation:

    import java.util.Scanner;
     
    public class Main{
     
    	public static void main(String args[]) {
    		Scanner myscanner = new Scanner(System.in);
     
    		System.out.println("Enter a number and press enter:");
     
    		double one = myscanner.nextDouble();
     
    		System.out.println("Enter some text and press enter:");
     
    		String text = myscanner.nextLine();
     
    		System.out.println("The number you entered: " + one);
    		System.out.println("The text you entered: " + text);
     
    	}
    }

    The above program won't work correctly because the nextDouble() function doesn't scan past the newline character *after* the number.

    However, your program works because nextDouble() does actually scan past the newline character *before* the number.

    Anyway, glad you got it figured out.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Empower (January 8th, 2014)

Similar Threads

  1. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  2. Replies: 2
    Last Post: August 30th, 2012, 09:45 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  5. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM