-3

Trying to make a simple calculator in Java but I keep getting errors when I try to compile it. Not sure if it's the coding or something I am doing when I compile it. Any help would be appreciated!

import java.util.Scanner;

class simple calculator { // simple calculator
public static void main(String args[]){
   System.out.println("My simple calculator\n");
       Scanner bucky= new Scanner(System.in);
   double fnum,snum,ans;
   System.out.print("Enter the first and second " +
           "number : ");
   a=bucky.nextFloat(); //assign the numbers
   b=bucky.nextDouble(); // to respective variable
   System.out.println("What operation u want to " +
                                   "perform");
   String operation;
   operation=bucky.next();
   switch(operation) {
   case "+":
       ans=a + b;
   System.out.print("Sum of the two inputs = ");
           System.out.print(ans);
           break;
   case "-":
       ans=a - b;
    System.out.print("Subtraction of the two inputs = ");
            System.out.print(ans);
            break;
   case "*":
       ans=a * b;
    System.out.print("Multiplication of the two inputs = ");
            System.out.print(ans);
       break;
   case "/":
       ans=a / b;
    System.out.print("Division of the two inputs = ");
            System.out.print(ans);
            break;
default : System.out.println("Give a proper operation " +
                        "symbol ");

                break; // not required
   }

}
}
  • 5
    welcome to the mind-readers club. You have passed our first test, we can not guess what the error is. – Scary Wombat Aug 18 '14 at 01:00
  • Would you mind telling us the exact errors compiler produces? – PM 77-1 Aug 18 '14 at 01:01
  • 1
    Did I saw a space in the name of your class? What is the name of the java file? – Volune Aug 18 '14 at 01:05
  • it says "simplecalculator.java:19: error: illegal character: '\u00a0' System.out.print(ans); – Tina Schreib Aug 18 '14 at 01:12
  • Does it for 4 - 20 as well. – Tina Schreib Aug 18 '14 at 01:13
  • Always put key information like that, and anything else of key import that would help us to understand your code and your problem, in your question itself. – Hovercraft Full Of Eels Aug 18 '14 at 01:33
  • I don't understand how this is a duplicate of something with different code and different problem, just because of a similar class name... @tina-schreib, **illegal character: '\u00a0'** means that you have some [no-break space](http://en.wikipedia.org/wiki/Non-breaking_space) [characters](http://www.fileformat.info/info/unicode/char/a0/index.htm) in your code. Try to get rid of them. – Volune Aug 18 '14 at 08:07

1 Answers1

0

Two compilation errors:

  • Your class name has whitespace in it: class simple calculator isn't valid, because "calculator" isn't a recognized token. Try: class SimpleCalculator. Also make it public and change the name of the file to match. (SimpleCalculator.java).
  • You declare variables a and b but don't give them types. Use float a = ... and double b = .... That one is a float and the other is a double is strange, but pressing on...

Other new-to-java issues to note:

  • You never close bucky, the scanner. This is a resource leak, and in a bigger application could be a major bug. Add bucky.close() after you read the last line from it, which is after operation = bucky.next()
  • the name bucky tells me absolutely nothing about what it is or what its use is. Try to use descriptive variable names. (The only main exception being single letter names for loop indices, etc).
  • fnum and snum are local variables that you declare but never use. Perhaps get rid of them?

Here's your code with edits made:

import java.util.Scanner;

public class SimpleCalculator { // simple calculator
    public static void main(String args[]){
        System.out.println("My simple calculator\n");
        Scanner bucky= new Scanner(System.in);
        double ans;
        System.out.print("Enter the first and second " +
                "number : ");
        float a=bucky.nextFloat(); //assign the numbers
        double b=bucky.nextDouble(); // to respective variable
        System.out.println("What operation u want to " +
                "perform");
        String operation;
        operation=bucky.next();
        bucky.close();
        switch(operation) {
        case "+":
            ans=a + b;
            System.out.print("Sum of the two inputs = ");
            System.out.print(ans);
            break;
        case "-":
            ans=a - b;
            System.out.print("Subtraction of the two inputs = ");
            System.out.print(ans);
            break;
        case "*":
            ans=a * b;
            System.out.print("Multiplication of the two inputs = ");
            System.out.print(ans);
            break;
        case "/":
            ans=a / b;
            System.out.print("Division of the two inputs = ");
            System.out.print(ans);
            break;
        default : System.out.println("Give a proper operation " +
                "symbol ");

        break; // not required
        }
    }
}

I was able to compile it in the terminal with no trouble.

Mshnik
  • 7,032
  • 1
  • 25
  • 38