Reputation: 181
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);
}
}
Upvotes: 0
Views: 1108
Reputation: 1097
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();
Upvotes: 0
Reputation: 2286
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
Upvotes: 0
Reputation:
You may simply use:
if(scan.hasNextInt()) {//<-- get in if its a number
int firstNumber = scan.nextInt();
}
Upvotes: 1