beckman
beckman

Reputation: 65

How to restart a java program

I'm very new to java and just playing around with the basics. I have made some kind of calculator. ( there are probably easier calculators, just worked with what came to my mind )

Here is my code:

import java.util.Scanner;

public class App {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);


        double second;

        System.out.println("Please tell me a number");

        double first = input.nextDouble();
        input.nextLine(); // Consume newline left-over

        System.out.println("You chose the number " + first);

        System.out
                .println("Now please choose what you want to do with the number. ( divide, add, multiply or subtract )");
        String text = input.nextLine();

        switch (text) {
        case "divide":
            System.out.println("What do you want to divide your number " + first + " with?");
            second = input.nextDouble();
            double resultDivide = first / second;
            System.out.println(first + " / " + second + " = " + resultDivide);
            break;
        case "add":
            System.out.println("What do you want to add to your number " + first + "?");
            second = input.nextDouble();
            double resultAdd = first + second;
            System.out.println(first + " + " + second + " = " + resultAdd);
            break;
        case "multiply":
            System.out.println("What do you want to multiply your number " + first + " with?");
            second = input.nextDouble();
            double resultMultiply = first * second;
            System.out.println(first + " * " + second + " = " + resultMultiply);
            break;
        case "subtract":
            System.out.println("What do you want to subtract your number " + first + " with?");
            second = input.nextDouble();
            double resultSubtract = first - second;
            System.out.println(first + " - " + second + " = " + resultSubtract);
            break;
        default:
            System.out.println("Sorry, I did not quite understand that. Please try again.");

        }

    }

So my question is: When it reaches default in the switch case statement, can I make the program restart from begginning?

Thanks in advance.

Upvotes: 1

Views: 3145

Answers (3)

李象茂
李象茂

Reputation: 1

I think that you need a while loop,e.g:

String text = input.readLine();
while(text != null) {
   switch(...)
} 

And more, i want to give you some suggestions:

  • you should check the "text" string you input if is null, if the "text" string is null, you will encounter the NullPointerException.
  • if you will use so many case statements, you must consider the design patten, the link is Replacing if else statement with pattern.

Upvotes: 0

ernest_k
ernest_k

Reputation: 45309

Seems like all you want to do is loop until the user has specified a valid operation. In that case, you could use a while loop, watching a variable:

import java.util.Scanner;

public class App {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        double second;
        System.out.println("Please tell me a number");
        double first = input.nextDouble();
        input.nextLine(); // Consume newline left-over

        System.out.println("You chose the number " + first);

        System.out
                .println("Now please choose what you want to do with the number. ( divide, add, multiply or subtract )");
        String text = input.nextLine();

        boolean correctOperation = true;

        do {
            correctOperation = true;

            switch (text) {
                case "divide":
                    System.out.println("What do you want to divide your number " + first + " with?");
                    second = input.nextDouble();
                    double resultDivide = first / second;
                    System.out.println(first + " / " + second + " = " + resultDivide);
                    break;
                case "add":
                    System.out.println("What do you want to add to your number " + first + "?");
                    second = input.nextDouble();
                    double resultAdd = first + second;
                    System.out.println(first + " + " + second + " = " + resultAdd);
                    break;
                case "multiply":
                    System.out.println("What do you want to multiply your number " + first + " with?");
                    second = input.nextDouble();
                    double resultMultiply = first * second;
                    System.out.println(first + " * " + second + " = " + resultMultiply);
                    break;
                case "subtract":
                    System.out.println("What do you want to subtract your number " + first + " with?");
                    second = input.nextDouble();
                    double resultSubtract = first - second;
                    System.out.println(first + " - " + second + " = " + resultSubtract);
                    break;
                default:
                    System.out.println("Sorry, I did not quite understand that. Please try again.");
                    correctOperation = false;
                    text = input.nextLine();
            }
        } while(!correctOperation)
    }
}

Upvotes: 1

sirdan13
sirdan13

Reputation: 103

A while loop is what you need. The enter condition of the loop will be the default case. Inside the loop you have to put this piece of code you used before:

System.out.println("Now please choose what you want to do with the number. ( divide, add, multiply or subtract )");
        String text = input.nextLine();

So it will continue to ask for the input until you don't get a default case.

Upvotes: 0

Related Questions