Nod
Nod

Reputation: 27

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

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.

Upvotes: 1

Views: 1051

Answers (2)

Adnan Azad
Adnan Azad

Reputation: 36

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.

Upvotes: 2

Aboelsebah
Aboelsebah

Reputation: 26

here is a code I wrote a few weeks ago, hope it helps you.

        int digits;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter an integer value: ");
    
        while (!scan.hasNextInt()) { //While loop for input validation
            System.out.print("This is not an integer, Please enter a valid value: ");
            scan.next();
        }
        digits = scan.nextInt();

Upvotes: 1

Related Questions