Results 1 to 4 of 4
  1. #1
    fatihkok is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default InputMismatchException Error

    Hello,
    I'm trying to do a simple calculator, but I have some problems about getting double input. I am running the code and enter the input (like 5+4) then it returns

    java.util.InputMismatchException
    Scanner.nextDouble(Unknown Source) errors.
    The error is shown when getting the firstNum //firstNum = input.nextDouble();
    Thank you for your time and helps.

    Here is my code:

    Java Code:
    import java.util.Scanner;
    
              public class Main {
    	
    	      public static void main(String[] args) {
              double firstNum;    // First number in the expression.
    	      double secondNum;   // Second number in the expression.
    	      char operator;      // The operator in the expression.
    	      double value;       // The value of the expression.
    	      
    	      System.out.println("Enter expressions such as  2 + 2  or  34.2 * 7.81");
    	      System.out.println("using any of the operators +, -, *, /.");
    	      System.out.println("To end, enter a 0.");
    	     
    	      Scanner input = new Scanner(System.in);
    	      
    	      while (true) {
    	          
    	          /* Get user's input, ending program if first number is 0. */
    	      
    	    	  System.out.println("? ");
    	          firstNum = input.nextDouble();
    	          
    	          if (firstNum == 0)
    	             break;
    	          operator = input.findInLine(".").charAt(0);
    	          secondNum = input.nextDouble();
    	          
    	          /* Compute the value of the expression. */
    	          
    	          switch (operator) {
    	              case '+':
    	                 value = firstNum + secondNum;
    	                 break;
    	              case '-':
    	                 value = firstNum - secondNum;
    	                 break;
    	              case '*':
    	                 value = firstNum * secondNum;
    	                 break;
    	              case '/':
    	                 value = firstNum / secondNum;
    	                 break;
    	              default:
    	            	  System.out.println("Unknown operator: " + operator);
    	                 continue;  // Back to start of loop!
    	          } // end switch
    	          
    	          /* Display the value. */
    	          
    	          System.out.println("Value is " + value);
    	          System.out.println();
    	                    
    	      } // end while
    	      
    	      System.out.println("Good bye");
        }
        }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: InputMismatchException Error

    Did you read the Scanner API doc for nextDouble()? 5+4 is not a valid number in double format. Either you read the whole expression with nextLine() and then try to identify the two numbers and the operand, or you read the three values individually.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    fatihkok is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: InputMismatchException Error

    Yes, I read that. I also used int instead of double but it again gives the same error. I think there is an assignment problem but I can not find it.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: InputMismatchException Error

    5+4 isn't an int either ist's a string literal. Read my previous reply.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. throw new InputMismatchException
    By Pat in forum New To Java
    Replies: 8
    Last Post: 10-09-2012, 09:53 AM
  2. InputMismatchException
    By gnbradford in forum New To Java
    Replies: 3
    Last Post: 09-22-2012, 09:45 PM
  3. InputMismatchException
    By jihad in forum New To Java
    Replies: 5
    Last Post: 12-17-2011, 03:10 PM
  4. Replies: 2
    Last Post: 02-11-2011, 09:03 AM
  5. try-catch InputMismatchException in a while loop
    By themulator in forum New To Java
    Replies: 17
    Last Post: 10-12-2010, 04:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •