Warm tip: This article is reproduced from serverfault.com, please click

How do I get user input validation into my java calculator program?

发布于 2020-11-27 01:30:15

So, I recently made a calculator in java as homework and I've gotten all the ways I can make it, down to a tee. I've done if statements and switch statements to allow the user to get different answers depending on which operator they used (/, *, +, -), however, I don't quite fully understand for, do and do-while loops yet to integrate it into my program.

This is my Calculator's source code. As you can see the switch statement prints "Invalid" if it's not an operator, however, I don't want the user to have to rerun the whole app, I want it to loop (and not for just the operator, but for the numbers as well)

import java.util.Scanner;

public class JCal
{
public static void main (String[] args)
{
    Scanner input = new Scanner(System.in);

    double num1, num2, divided, multiplied, added, subtracted;
    char operator;

    System.out.print("Value A: ");
    num1 = input.nextDouble();

    System.out.print("Operator (/, *, +, -): ");
    operator = input.next().charAt(0);

    System.out.print("Value B: ");
    num2 = input.nextDouble();

    divided = num1 / num2;
    multiplied = num1 * num2;
    added = num1 + num2;
    subtracted = num1 - num2;

    switch (operator)
    {
        case '/':
        {
            System.out.println("Result: " + divided);
            break;
        }
        case '*':
        {
            System.out.println("Result: " + multiplied);
            break;
        }
        case '+':
        {
            System.out.println("Result: " + added);
            break;
        }
        case '-':
        {
            System.out.println("Result: " + subtracted);
            break;
        }
        default:
        {
            System.out.println("Invalid operator!");
            break;
        }
    }
}

}

I tried something like:

double num1;
Scanner input = new Scanner(System.in);

do
        {
            System.out.println("Value A: ");
            while (! input.hasNextDouble());
            {
                System.out.println("Invalid number!");
                input.nextDouble();
            }
        } while (num1 = input.hasNextDouble());

That's where I get stuck, the while (num1 = input.hasNextDouble()); is a boolean and a double clashing, according to IntelliJ (I assume because num1 is a double and the .hasNext function gives a boolean, they don't go together.) so IntelliJ underlines it red.

THANK YOU SO MUCH, if you took time out of your day to help me with this probably trivial problem.

Questioner
Nod
Viewed
0
Adnan Azad 2020-11-30 01:20:21

Perhaps you could create a 'bool Exit' variable and initially set it to False

put your entire code (except for main obviously) in a do while loop with the condition being if until exit is true

main {
   do {
      ... your code
   }while(Exit != True)
}

So after every run at the end you can ask the user to try another calculation or not (yes/no) and if no, set Exit to true and your program will come out of the loop. if yes set Exit to False and your program will loop again.