how to restrict input only as numbers in java

Viewed 57

I'm coding a calculator just for fun. I got through most of the errors except for one, how to restrict input only to numbers. While executing the program I realized that if I input an alphabetic character and not a number, it gave me an error. Can anyone help me out on making a loop so that if a non-number is typed, the loop will continue until a number is typed? Here's the code of the calculator:

import java.text.DecimalFormat;
import java.util.Scanner;
class Calculator
{
    public static void main(String arg[])
    {
        try (
        Scanner Number$1 = new Scanner(System.in)) 
        {
            double Number1;
            System.out.println("Enter Number 1"); 
            Number1= Number$1.nextDouble();
            try (
            Scanner Number$2 = new Scanner(System.in)) 
            {
                double Number2;
                System.out.println("Enter Number 2"); 
                Number2 = Number$2.nextDouble();
                //to make ui better
                String a= "The addition of the two numbers is: ";
                String b= "The subtraction of the two numbers is: ";
                String c= "The multiplication of the two numbers is: ";
                String d= "The division of the two numbers is: ";
                String Options= "";
                try (Scanner scan = new Scanner(System.in)) {
                    System.out.println("Do You Want To Add, Subtract, Multiply, Divide? ");
                    System.out.println("a: Add");
                    System.out.println("b: Subtract");
                    System.out.println("c: Multiply");
                    System.out.println("d: Divide");
                    Options = scan.nextLine();
                }
                double add= Number1+Number2;
                double sub= Number1-Number2;
                double multi= Number1*Number2;
                double div= Number1/Number2;
                if(Options.equals("a"))
                {
                    if(((add*10)%10==0)==true)
                    {
                        DecimalFormat format= new DecimalFormat();
                        format.setDecimalSeparatorAlwaysShown(false);
                        System.out.println(a+(format.format(add)));
                    }
                    if(((add*10)%10==0)==false)
                    {
                        System.out.println(a+(add));
                    }
                }
                if(Options.equals("b"))
                {
                    if(((sub*10)%10==0)==true)
                    {
                        DecimalFormat format= new DecimalFormat();
                        format.setDecimalSeparatorAlwaysShown(false);
                        System.out.println(b+(format.format(sub)));
                    }
                    if(((sub*10)%10==0)==false)
                    {
                        System.out.println(b+(sub));
                    }
                }
                if(Options.equals("c"))
                {
                    if(((multi*10)%10==0)==true)
                    {
                        DecimalFormat format= new DecimalFormat();
                        format.setDecimalSeparatorAlwaysShown(false);
                        System.out.println(c+(format.format(multi)));
                    }
                    if(((multi*10)%10==0)==false)
                    {
                        System.out.println(c+(multi));
                    }
                }
                if(Options.equals("d"))
                {
                    if(((div*10)%10==0)==true)
                    {
                        DecimalFormat format= new DecimalFormat();
                        format.setDecimalSeparatorAlwaysShown(false);
                        System.out.println(d+(format.format(div)));
                    }
                    if(((div*10)%10==0)==false)
                    {
                        System.out.println(d+(div));
                    }
                }
            }
        }     
    }
}
1 Answers

Try this:

double number1;
double number2;
boolean validInput = false;
Scanner scanner = new Scanner(System.in))
while(!validInput){
    System.out.println("Enter Number 1");
    if(scanner.hasNextDouble()){
        validInput = true;
        number1 = scanner.nextDouble();
    }
}

validInput = false;
while(!validInput){
    System.out.println("Enter Number 2");
    if(scanner.hasNextDouble()){
        validInput = true;
        number2 = scanner.nextDouble();
    }
}

A few notes on code style:

  • It's generally bad practice to have multiple scanners listening to System.in

  • The naming convention for variables follows camelCase, so 'number1' not 'Number1', but it's not a huge deal.

  • For code readability, consider adding comments explaining the conditions inside the nested if-blocks.

Related