[ERROR] Java Calculator w/ basic operations

0 Lauren Barry · December 20, 2014
So, I'm starting out with Java, and I wanted to make a basic calculator similar to Bucky's, but with all other basic mathematical operations (add, subtract, multiply, divide). I'm getting an error (see below). I'm not sure what is wrong with my code. Could someone maybe help me?


The console, along with the error I'm currently getting:
Welcome to Lauren's Calculator
 
Input the first number: 
5
ADD, SUBTRACT, MULTIPLY, or DIVIDE?
Input the second number: 
ADD
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 calculator.main(calculator.java:14)

The int "5" and string "ADD" are input I tried to enter. Everything else is either an error or output of the code.



My code:
import java.util.Scanner;
public class calculator {

public static void main(String[] args) {
// firstNum, secNum, answer, and operation(string)
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Lauren's Calculator");
System.out.println(" ");
System.out.println("Input the first number: ");
double firstNum = input.nextDouble();
System.out.println("ADD, SUBTRACT, MULTIPLY, or DIVIDE?");
String operation = input.nextLine();
System.out.println("Input the second number: ");
double secNum = input.nextDouble();
double answer = 0;
switch(operation) {
case("ADD"):
answer = firstNum + secNum;
break;
case("SUBTRACT"):
answer = firstNum - secNum;
break;
case("MULTIPLY"):
answer = firstNum * secNum;
break;
case("DIVIDE"):
answer = firstNum / secNum;
break;
default:
System.out.println("ERROR UNKOWN OPERATION");
break;
}
System.out.println("The answer is " + answer);
}

}




Thank you in advance! :P

Post a Reply

Replies

Oldest  Newest  Rating
+1 Alf Nohr · December 20, 2014
It's a fast fix, all you need to do is to change
String operation = input.nextLine();

to
String operation = input.next();

:)
0 Faysal Ahmed · December 20, 2014
It's quite common error. Every newbie faces this problem. You can see that

System.out.println("Input the second number: ");



this line is executing before you enter input. 

The solution is simple.

after 

System.out.println("ADD, SUBTRACT, MULTIPLY, or DIVIDE?");


 line , just include this line

input.nextLine();



Google yourself to find out the reason, you'll never forget then.
0 KEVIN K P · December 20, 2014
SOLUTION 1

Add the line input.nextLine() before System.out.println("ADD, SUBTRACT, MULTIPLY, or DIVIDE?");

SOLUTION 2

CHANGE String operation = input.nextLine();  to String operation = input.next();




Happy coding  Lauren  :)
0 Ethan Hannum · December 20, 2014
I did mine a little different then yours and mine works... see if anything from mines help :D


import java.util.Scanner;

public class apple {
    public static void main(String args[]){
    Scanner bucky = new Scanner(System.in);
    int operator, fnum, snum, answer;
    System.out.println("Enter your Operation addition (1), subtraction (2), multiplcation (3), divition (4)");
    operator = bucky.nextInt();
   
    switch (operator){
    case 1:
    System.out.println("You are using +.");
       break;
    case 2:
    System.out.println("You are using -.");
            break;
    case 3:
    System.out.println("You are using *.");
    break;
    case 4:
    System.out.println("You are using /.");}
   
    if (operator == 1){
    System.out.println("Enter First Number.");
    fnum = bucky.nextInt();
    System.out.println("Enter Second Number.");
    snum = bucky.nextInt();
    answer = fnum + snum;
    System.out.println(answer);
    }else{
    if (operator == 2){
    System.out.println("Enter First Number.");
    fnum = bucky.nextInt();
    System.out.println("Enter Second Number.");
    snum = bucky.nextInt();
    answer = fnum - snum;
    System.out.println(answer);
    }else{
    if (operator == 3){
    System.out.println("Enter First Number.");
    fnum = bucky.nextInt();
    System.out.println("Enter Second Number.");
    snum = bucky.nextInt();
    answer = fnum * snum;
    System.out.println(answer);
    }else{
    System.out.println("Enter First Number.");
    fnum = bucky.nextInt();
    System.out.println("Enter Second Number.");
    snum = bucky.nextInt();
    answer = fnum / snum;
    System.out.println(answer);
   
    }
    }
   
    }
    }
}
  • 1

Java / Android Development

106,913 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator