Results 1 to 2 of 2
  1. #1
    Propinquity is offline Member
    Join Date
    Oct 2013
    Posts
    63
    Rep Power
    0

    Default Help with simple calculator?

    For some reason, I keep receiving the fatal 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.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at calculator.calculator.main(calculator.java:14)

    I'm not really sure why this is happening. Could someone help explain to me what's going on?

    package calculator;

    import java.util.Scanner;

    public class calculator {
    public static void main(String[] args) {
    int a, b, answer;
    String operator;

    Scanner s = new Scanner(System.in);

    derp("What operator do you want to use? Ex: +, -, *, /, etc");
    a = s.nextInt();
    operator = s.next();
    b = s.nextInt();
    if(operator.contains("+")){
    answer = a + b;
    derp(""+answer);
    }
    if(operator.contains("-")){
    answer = a - b;
    derp(""+answer);
    }
    if(operator.contains("*")){
    answer = a * b;
    derp(""+answer);
    }
    if(operator.contains("/")){
    answer = a / b;
    derp(""+answer);
    }
    }
    public static void derp(String s) {
    System.out.println(s);
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Help with simple calculator?

    Please wrap your code in
    Java Code:
     tags
    so it retains its formatting.

    Also point out the line that that exception is thrown from.
    And show us what input (exactly) is being used.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Rate my app : Simple Calculator
    By Akar-Slemany in forum New To Java
    Replies: 4
    Last Post: 03-23-2013, 12:16 AM
  2. Simple calculator
    By itaiyz97 in forum New To Java
    Replies: 2
    Last Post: 09-24-2012, 06:21 PM
  3. Trying to make simple calculator
    By Buldogg in forum New To Java
    Replies: 1
    Last Post: 09-18-2011, 02:37 PM
  4. Simple Calculator
    By marksey07 in forum New To Java
    Replies: 12
    Last Post: 01-09-2011, 07:30 AM
  5. creating a simple calculator
    By hobo in forum New To Java
    Replies: 4
    Last Post: 11-09-2009, 04:09 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
  •