1
votes

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");
        }
    }
}
2
Add code here and use {} to format. - TheLostMind
Programs terminate when they've completed their instructions. It's expected. - David
You're testing that the scanner is equal to a string. It never will be. use #next(). - Sam McCreery
Disagree with the close votes. The desired behavior is clear, and the issue with the code is easy to spot. Be nice to new users. - Craig Otis
@CraigOtis The question used to have zero code in it. - Andrew

2 Answers

4
votes

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();
0
votes

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!