0

So I'm trying to make a simple calculator.

How do I make when I enter the first number, it works but if I insert "abc" it will give me an error.

How I make it in order when you write "abc" to say " please enter a number "

import java.util.Scanner;

public class calculator 
{
    public static void main(String[] args0) {
        Scanner test = new Scanner(System.in);

        int  x; 
        int  y;
        String  c;

        System.out.println("Insert a number ");
        x = test.nextInt();

        System.out.println("insert a value e.g * / + -");
        c = test.next();

        System.out.println("Insert another number");
        y = test.nextInt();

        if ( c.equals("*")) {
            System.out.println("the total is " + x*y);
        } 

        if (c.equals("+")) {
            System.out.println("the total is " + (x+y));

        }

        if (c.equals("-")) {
            System.out.println("the total is "+ (x-y));
        }

        if (c.equals("/")) {
            System.out.println("the total is "+ (x/y));
        }

    }
}
Didix
  • 567
  • 1
  • 7
  • 26
  • you may want to read up on 'Exception handling'. Or, read in Strings, and check if they are numerical, until they are, let the user enter new data – Stultuske May 31 '18 at 13:13

5 Answers5

5

You can verify the input until be a int using a scanner property Scanner.hasNextInt()

Scanner scanner = new Scanner(System.in);
System.out.print("Enter number 1: ");
while (!scanner.hasNextInt()) scanner.next();

Example:

public static void main(String[] args0) {
        Scanner test = new Scanner(System.in);

        int  x; 
        int  y;
        String  c;

        System.out.println("Insert a number ");
        while (!test .hasNextInt()) test .next(); // Scanner Validation
        int x = test .nextInt();

}

JavaDoc of Scanner

Lino
  • 19,604
  • 6
  • 47
  • 65
Allan Braga
  • 460
  • 5
  • 19
  • 1
    you could use a `do-while` loop and then print the `"Insert a number..."`-prompt on every iteration – Lino May 31 '18 at 13:35
0

The error you get is an exception. You can actually "catch" your exceptions, so that when they appear, your program doesn't break, and you can do what is in place for that error (output a "Please, insert only numeric values" feedback?)

You can find some info on try-catch blocks here try-catch blocks

Jorge.V
  • 1,329
  • 1
  • 13
  • 19
0

Try this:

boolean success = false;
while (!success) {
    try {
        y = test.nextInt();
        success = true;
    } catch (InputMismatchException e) {
        test.nextLine();
        System.out.println("Please enter a number.");
    }
}
Ahmad Shahwan
  • 1,662
  • 18
  • 29
0

If you're willing to accept doubles instead of ints, java doubles have a built in method isNaN(), where NaN stands for Not a Number.

if (Double.isNaN(doubleValue)) {
    ...
}
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
Amanda
  • 314
  • 3
  • 12
0

Try this:

public static void main(String[] args) {
        // TODO Auto-generated method stub
         Scanner test = new Scanner(System.in);

            int  x; 
            int  y;
            String  c;

            try {
                System.out.println("Insert a number ");
                x = test.nextInt();

                System.out.println("insert a value e.g * / + -");
                c = test.next();

                System.out.println("Insert another number");
                y = test.nextInt();

                if (c.equals("*")) {
                    System.out.println("the total is " + x*y);
                } 

                if (c.equals("+")) {
                    System.out.println("the total is " + (x+y));
                }

                if (c.equals("-")) {
                    System.out.println("the total is "+ (x-y));
                }

                if (c.equals("/")) {
                    System.out.println("the total is "+ (x/y));
                }

            } catch(InputMismatchException e) {
                System.out.println("Please enter correct values.");
            }

    }

Modifications:

  1. The error you are getting is known as RunTime Error or Exceptions due to wrong input type. In order to handle RunTime Exceptions, You need to use try and catch block.

  2. try and catch blocks are used to handle RunTime Exceptions. If any error or exception occurs within try block then It will be thrown to catch block to be handled instead of terminating your program.

Nitin Bisht
  • 5,053
  • 4
  • 14
  • 26