0

I'm trying to create a simple program that takes two numbers from user input and adds them together. I've got it down for the correct use however I'm trying to stop crashes if the user enters something else, like a string. How can I check the input without exceptions as I hear they are bad practice?

Ideally I'd want it to just repeat the initial question of asking for a number to be inputted. I'm assuming that would be within a loop?

I.E.

Ask user firstNumber if not int, repeat

Ask user secondNumber if not int, repeat

Do math

edit: So I'm nearly there with the help so far. I've used some do-while loops to get it to repeat the question until the input is an integer. However now It keeps repeating the first while-do loop even if I enter an integer. If I enter a something != integer it seems to skip to the second while-do then cease.

EDIT: NEW CODE

import java.util.Scanner;

public class App {

    public static void main(String[] args) {

       Scanner scan;

    scan = new Scanner(System.in);
    int firstNumber = Integer.MIN_VALUE;
    int secondNumber = Integer.MIN_VALUE;

    do {
        System.out.print("Enter the first number to be added: ");
        if (scan.hasNextInt()) {
            firstNumber = scan.nextInt();
        }
    }
    while(firstNumber != Integer.MIN_VALUE);


    do {
        System.out.print("Enter the second number to be added: ");
        if (scan.hasNextInt()) {
            secondNumber = scan.nextInt();
        }
    }
    while(secondNumber != Integer.MIN_VALUE);

    if ((firstNumber != Integer.MIN_VALUE) && (secondNumber != Integer.MIN_VALUE)) {
        int sum = secondNumber + firstNumber;
        System.out.println("That equals: " + sum);
    }
    scan.close();
    }
}

OLD CODE

import java.util.Scanner;

public class calculate {
    private static Scanner scan;

    public static void main(String[] args) {

        scan = new Scanner(System.in);

        System.out.print("Enter the first number to be added: ");
        int firstNumber = scan.nextInt();

        System.out.println("Enter the second number to be added: ");
        int secondNumber = scan.nextInt();

        int sum = secondNumber + firstNumber;

        System.out.println("That equals: "+ sum);
    }
}
5
  • The easiest way is to use a try catch block. Commented Dec 1, 2014 at 12:10
  • @YanFoto I'm pretty new to Java, but I guess I'll go searching for try catch. Commented Dec 1, 2014 at 12:12
  • 1
    You should catch InputMismatchException Commented Dec 1, 2014 at 12:16
  • Don't edit your question to make it about something else. That voids everyone's answer. Ask a new question if you still don't understand something. Commented Dec 1, 2014 at 15:02
  • @Teepeemm The first paragraph was the edit. No one has answered my question yet. Ill arrange the edit to be at the bottom instead. It says to edit the initial question if there is new information to be added and not to open new question Commented Dec 1, 2014 at 15:06

3 Answers 3

1

You may simply use:

if(scan.hasNextInt()) {//<-- get in if its a number
  int firstNumber = scan.nextInt();
}
Sign up to request clarification or add additional context in comments.

Comments

0

The following should work:

    Scanner scan;

    scan = new Scanner(System.in);
    int firstNumber = Integer.MIN_VALUE;
    int secondNumber = Integer.MIN_VALUE;

    while (firstNumber == Integer.MIN_VALUE) {
        System.out.print("Enter the first number to be added: ");
        if (scan.hasNextInt()) {
            firstNumber = scan.nextInt();
        } else {
            scan.next();
        }
    }

    while (secondNumber == Integer.MIN_VALUE) {
        System.out.print("Enter the second number to be added: ");
        if (scan.hasNextInt()) {
            secondNumber = scan.nextInt();
        } else {
            scan.next();
        }
    }

    if ((firstNumber != Integer.MIN_VALUE) && (secondNumber != Integer.MIN_VALUE)) {
        int sum = secondNumber + firstNumber;
        System.out.println("That equals: " + sum);
    }
    scan.close();

1 Comment

updated my question to reflect a few changes, any ideas?
0

You can use 'try catch' .

   boolean flag=false;

   do
   {
       try
        {
         into firstno=scan.nesting();
         flag=true;
        }
          catch(Exception e)
             { 
             System.out.print("please insert only           number");
              }
      }while(flag!=true);

Using a try catch you can catch exception and the message

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.