-2

Need help with this: Here is my code: http://pastebin.com/7aG0xbhJ Couldn't figure out how to post it here. Just keeps saying terminated. Trying to create a calculator.

import java.util.Scanner;
public class Calculator {
    public static void main(String args[]) {
            Scanner input=new Scanner(System.in);
            System.out.println("Welcome to My Multi Calculator");
            System.out.println("Here are the choices:");
            System.out.println("(1) Convert Pounds to Kilograms");
            System.out.println("(2) Convert USD to Euro");
            System.out.println("(3) Convert Degrees F to C");
            System.out.println("(4) Calculate 20% gratuity on a bill");
            System.out.println("(5) Calculate if a number is prime");
            System.out.println("(6) Calulate the absolute difference of two numbers");
           System.out.println("(7) Quit");

        if (input.equals("1")) {
            //System.out.println("1");
            System.out.println("Input amount:");
            double size = input.nextInt();
            System.out.println("Answer: "+ size*0.453592);
        }
        if (input.equals("2")) {
            System.out.println("2");
        }
        if (input.equals("3")) {
            System.out.println("3");
        }
        if (input.equals("4")) {
            System.out.println("4");
        }
        if (input.equals("5")) {
            System.out.println("5");
        }
        if (input.equals("6")) {
            System.out.println("6");
        }
        if (input.equals("7")) {
            System.out.println("7");
        }
    }
}
Codor
  • 17,447
  • 9
  • 29
  • 56
Jokes
  • 49
  • 1
  • 9

2 Answers2

4

Like said, you are testing if Scanner object equals to String instance, which is never true, as they are completely different kinds of objects.

You'd want to replace this:

Scanner input=new Scanner(System.in);
// printing here
if (input.equals(...

with this:

Scanner scanner = new Scanner(System.in);
// printing here
String input = scanner.nextLine();
if (input.equals(...

Addition: of course when you do that, you also need to change other references like

double size = input.nextInt();

to use your scanner instance:

double size = scanner.nextInt();
David
  • 208,112
  • 36
  • 198
  • 279
eis
  • 51,991
  • 13
  • 150
  • 199
  • if(input.equals("1")) { //System.out.println("1"); System.out.println("Input amount:"); double size = input.nextInt(); System.out.println("Answer: "+ size*0.453592); } – Jokes Nov 17 '15 at 18:38
  • input.nextInt gives error when I do that do the first one. Which is converting pounds to kilograms – Jokes Nov 17 '15 at 18:39
  • Error is "The method nextInt() is undefined for the type String" – Jokes Nov 17 '15 at 18:39
  • 1
    Additionally a loop as suggested by @Brandon Laidig should be used to process more than a single input. – Wormbo Nov 17 '15 at 18:39
  • @Jokes sure it will, that's the next thing you need to fix. I mean, if you have a scanner object and a String named `input`, you can't call nextInt() on a string. – eis Nov 17 '15 at 18:43
  • @Wormbo given that OP *wants* to process more than single input. I haven't seen such a requirement from him yet. – eis Nov 17 '15 at 18:43
  • @eis Well, the option 7 sort-of suggests it, considering any other key would also quit without a loop. – Wormbo Nov 17 '15 at 18:45
  • @Wormbo Might be, might not be. It's anyway unrelated to the current problem. – eis Nov 17 '15 at 18:47
  • @Jokes: There was a typo in the answer's code. You'd call methods like `nextInt()` on `scanner`, not on `input` in this case. Basically, you have a `Scanner` object which is used to collect user input. You can store that user input in variables and compare those variable. But you always need to collect further user input from the `Scanner`. – David Nov 17 '15 at 19:26
  • @David no, there wasn't a typo in the answer. Please re-read the answer and roll back your edit. – eis Nov 17 '15 at 19:26
  • @eis: You're right, my mistake. Though I suspect Jokes made the same mistake :) – David Nov 17 '15 at 19:28
  • @David I don't think so since that part was added to the answer only after Jokes commented about his mistake :P – eis Nov 17 '15 at 19:28
0

You're on the right track, but try to think of it in a broader sense. You want your program to stop quitting, right? What type of logic could you use to implement that behavior?

What you want is for your program to loop until a certain argument is given, thus causing it to conclude. Something like the following should work

while(!"USER_INPUT".equals("7")) {
    //continue checking values
}

That will allow your loop where you check the values to continue to run, so long as what the user enters isn't equal to your exit case. You'll also want to make sure that within that loop, that you prompt the user for another input. Without it, you'll just get stuck in that loop without anything changing.

I hope this helps and good luck!

Brandon Laidig
  • 430
  • 2
  • 14
  • actually, I think you are leading OP to wrong direction. Doing it the way you suggest would lead to infinite loop with current code. – eis Nov 17 '15 at 18:32
  • That's why I mentioned that they'll need to make sure they update the value from within the loop. How else do you suggest to keep this program running for more than 1 iteration? – Brandon Laidig Nov 17 '15 at 18:33
  • `input` in the OP's code is the scanner itself, not its scanned input. Comparing it for equality with a string will always fail. – Wormbo Nov 17 '15 at 18:38
  • I've updated my answer to remove the input check, however I feel the overlying logic behind it stands tall. Without some sort of loop, the asker's program will continue to terminate. Which I would assume to not be ideal when trying to create a calculator – Brandon Laidig Nov 17 '15 at 18:40