I'm attempting to create a program where the user enters a five-digit zip code from 00000 to 99999 and if the user enters a number out of that range or non-numeric values, it should throw an exception and give the user a chance to keep trying until they enter 5 numeric values.
My program only seems to catch the first instance of it, and afterwards it simply prints out the 2nd thing the user enters even if it doesn't fit the requirements.
I've just been stumped, and I'm not sure how I can use a while loop with my code, though I believe that's what I may need maybe?
I'm a beginner at this and any help would be appreciated!
import java.util.InputMismatchException;
import java.util.Scanner;
public class xzip_code {
public static void main(String[] args)
{
try
{
Bounds(Program());
}
catch(IllegalArgumentException ex)
{
System.out.println("Enter 5 Digits");
Program();
}
catch(InputMismatchException ex)
{
System.out.println("Enter Numbers");
Program();
}
}
public static void Bounds(String answer)
{
int length = answer.length();
if(length<5 || length>5)
{
throw new IllegalArgumentException("Enter 5 Digits");
}
char a = answer.charAt(0);
char b = answer.charAt(1);
char c = answer.charAt(2);
char d = answer.charAt(3);
char e = answer.charAt(4);
int f = a;
int g = b;
int h = c;
int i = d;
int j = e;
if(f>58 || g>58 || h>58|| i>58||j>58)
{
throw new InputMismatchException("Enter Numbers");
}
}
public static String Program()
{
Scanner userInput = new Scanner(System.in);
String x = userInput.next();
System.out.println(x);
return x;
}
}
Your method Bounds() does the validation work.
Currently, in your catch block you are just calling Program(). Instead you need to call the Bounds() and pass parameter Program() to it.
The below code will loop until there is no exception (successful try block execution).
boolean flag = true;
while(flag) {
try {
Bounds(Program());
flag = false;
} catch(IllegalArgumentException ex) {
System.out.println("Enter 5 Digits");
}
catch(InputMismatchException ex) {
System.out.println("Enter Numbers");
}
}
You also need to check if user has entered only numbers.
ASCII value of 0 -> 48 and 9 -> 57. Therefore, your check of > 58 makes no sense. It should check within the range.
You can simple use if (Character.isLetter(answer.charAt(index))) to check for individual digits (which is tedious).
Instead, just convert the String to Integer and check if it successfully gets converted otherwise throw error.
try {
Integer.parseInt(answer);
} catch (NumberFormatException e) {
throw new InputMismatchException("Enter Numbers");
}
You need to call Bounds(Program()); untill your program throws no error.
For that I created while loop that verifies if boolean isError is true.
To check if entered char is digit you can use Character.isDigit method.
See correct code:
package com.stackoverflow.main;
import java.util.InputMismatchException;
import java.util.Scanner;
public class xzip_code {
public static void main(String[] args) {
System.out.println("Enter 5 Digits");
boolean isError = true;
while (isError) {
try {
Bounds(Program());
} catch (IllegalArgumentException ex) {
System.out.println("Enter 5 Digits");
continue;
} catch (InputMismatchException ex) {
System.out.println("Enter Numbers");
continue;
}
isError = false;
}
}
public static void Bounds(String answer) {
int length = answer.length();
if (length < 5 || length > 5) {
throw new IllegalArgumentException("Enter 5 Digits");
}
char a = answer.charAt(0);
char b = answer.charAt(1);
char c = answer.charAt(2);
char d = answer.charAt(3);
char e = answer.charAt(4);
if (!(Character.isDigit(a) && Character.isDigit(b) && Character.isDigit(c) && Character.isDigit(d)
&& Character.isDigit(e))) {
throw new InputMismatchException("Enter Numbers");
}
}
public static String Program() {
Scanner userInput = new Scanner(System.in);
String x = userInput.next();
System.out.println(x);
return x;
}
}
Prints:
Enter 5 Digits
ewewdsddd
ewewdsddd
Enter 5 Digits
dffdffg
dffdffg
Enter 5 Digits
443446665
443446665
Enter 5 Digits
4444q
4444q
Enter Numbers
33333
33333
You need to make your catch a recursive call. The way you wrote it, it is caught, tries again, then ends.
Try to do it like this.
void foo() {
try {
bar();
} catch (Exception e) {
// try again
foo();
}
}
It also may be a good idea to keep track of how many times you retry. This could easily cause a StackOverflowError if you get it wrong too many times. I want to say the number is about 8 or 9 thousand.
Another option would be to use a loop.
void foo() {
boolean success = false;
while(!success) {
success = tryFoo();
}
}
boolean tryFoo() {
try {
bar();
return true; // true for success
} catch (Exception e) {
return false; // false for failed
}
}
Related
There are several questions I would like to ask, please refer the comment part I have added in the code, Thanks.
package test;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test {
/* Task:
prompt user to read two integers and display the sum. prompt user to read the number again if the input is incorrect */
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean accept_a = false;
boolean accept_b = false;
int a;
int b;
while (accept_a == false) {
try {
System.out.print("Input A: ");
a = input.nextInt(); /* 1. Let's enter "abc" to trigger the exception handling part first*/
accept_a = true;
} catch (InputMismatchException ex) {
System.out.println("Input is Wrong");
input.nextLine(); /* 2. I am still not familiar with nextLine() parameter after reading the java manual, would you mind to explain more? All I want to do is "Clear Scanner Buffer" so it wont loop for the println and ask user to input A again, is it a correct way to do it? */
}
}
while (accept_b == false) {
try {
System.out.print("Input B: ");
b = input.nextInt();
accept_b = true;
} catch (InputMismatchException ex) { /*3. Since this is similar to the above situation, is it possible to reuse the try-catch block to handling b (or even more input like c d e...) exception? */
System.out.println("Input is Wrong");
input.nextLine();
}
}
System.out.println("The sum is " + (a + b)); /* 4. Why a & b is not found?*/
}
}
I am still not familiar with nextLine() parameter after reading the java manual, would you mind to explain more? All I want to do is "Clear Scanner Buffer" so it wont loop for the println and ask user to input A again, is it a correct way to do it?
The use of input.nextLine(); after input.nextInt(); is to clear the remaining content from the input stream, as (at least) the new line character is still in the buffer, leaving the contents in the buffer will cause input.nextInt(); to continue throwing an Exception if it's no cleared first
Since this is similar to the above situation, is it possible to reuse the try-catch block to handling b (or even more input like c d e...) exception?
You could, but what happens if input b is wrong? Do you ask the user to re-enter input a? What happens if you have 100 inputs and they get the last one wrong?You'd actually be better off writing a method which did this for, that is, one which prompted the user for a value and returned that value
For example...
public int promptForIntValue(String prompt) {
int value = -1;
boolean accepted = false;
do {
try {
System.out.print(prompt);
value = input.nextInt();
accepted = true;
} catch (InputMismatchException ex) {
System.out.println("Input is Wrong");
input.nextLine();
}
} while (!accepted);
return value;
}
Why a & b is not found?
Because they've not been initialised and the compiler can not be sure that they have a valid value...
Try changing it something more like.
int a = 0;
int b = 0;
Yes, it's okay. And will consume the non-integer input.
Yes. If we extract it to a method.
Because the compiler believes they might not be initialized.
Let's simplify and extract a method,
private static int readInt(String name, Scanner input) {
while (true) {
try {
System.out.printf("Input %s: ", name);
return input.nextInt();
} catch (InputMismatchException ex) {
System.out.printf("Input %s is Wrong%n", input.nextLine());
}
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = readInt("A", input);
int b = readInt("B", input);
System.out.println("The sum is " + (a + b));
}
I have put comment to that question line.
package test;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean accept_a = false;
boolean accept_b = false;
int a=0;
int b=0;
System.out.print("Input A: ");
while (accept_a == false) {
try {
a = input.nextInt(); // it looks for integer token otherwise exception
accept_a = true;
} catch (InputMismatchException ex) {
System.out.println("Input is Wrong");
input.next(); // Move to next other wise exception // you can use hasNextInt()
}
}
System.out.print("Input B: ");
while (accept_b == false) {
try {
b = input.nextInt();
accept_b = true;
} catch (InputMismatchException ex) {
System.out.println("Input is Wrong");
input.next();
}
}
System.out.println("The sum is " + (a + b)); // complier doesn't know wheather they have initialised or not because of try-catch blocks. so explicitly initialised them.
}
}
Check out this "nextLine() after nextInt()"
and initialize the variable a and b to zero
nextInt() method does not read the last newline character.
I would like the program to re-do the while loop when it catches the exception - the exception being receiving a number zero. Instead it continues a while loop with the code below, I would like it to ask for the user input again until the user inputs a number that is different by zero.
import java.util.InputMismatchException;
import java.util.Scanner;
public class whilePerjashtim {
public static int division(int a, int b){
return a/b;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a, b;
System.out.println("Enter a value: ");
a = s.nextInt();
while(true){
try
{
System.out.println("Enter b value");
b = s.nextInt();
System.out.println("Sum of division is: " + division(a,b));
}
catch(ArithmeticException e)
{
System.err.println("Don't divide by zero!!!");
}
catch (java.util.InputMismatchException e)
{
System.err.println("Enter just a Number!!!");
}
finally
{
System.out.println();
}
}
}
}
Use something of the following form (not exact Java for your homework problem)
boolean validInput = false;
while (!validInput) {
.. get input
.. set validInput = true if no error
.. catch error
.. print try again message
}
You can set a boolean value, that determines, if the while loop ends succesfully. Then in every loop you start by assuming the value is true and when an exception is raised, you set it to false.
boolean success = false;
while(success == false){
success = true;
try {
System.out.println("Enter b value");
b = s.nextInt();
System.out.println("Sum of divison is: " + division(a,b));
}
catch(ArithmeticException e) {
System.err.println("Dont divide by zero!!!");
success = false;
}
}
Define a boolean outside of your while loop, and use it for the while's condition.
Assuming I understood your question correctly, you want to stay in the loop if the user's input threw an exception, ie it was invalid input, and you want to break out of the loop when you get valid input from the user.
boolean gotValidInput = false;
while (!gotValidInput) {
try {
System.out.println("Enter b value");
b = s.nextInt();
gotValidInput = true;
System.out.println("Sum of divison is: " + division(a,b));
} catch(ArithmeticException e) {
System.err.println("Dont divide by zero!!!");
} catch (java.util.InputMismatchException e) {
System.err.println("Enter just a Number!!!");
} finally {
System.out.println();
}
}
In this implementation, your two exceptions would both be thrown before gotValidInput = true; gets evaluated, so it would only get set to true if no exceptions were thrown.
You can put extra outter loop, like
while (true) {
System.out.println("Enter a value: ");
a = s.nextInt();
while(true) {
/// terminate the loop in case of problem with a, and allow a user to re done
}
}
Cleaned up the warnings and moved s to ouside the main method and defined it as static. It appears the s is a resource leak if within the main and is never closed.
import java.util.InputMismatchException;
import java.util.Scanner;
public class whilePerjashtim {
private static Scanner s;
public static int division(int a, int b) {
return a / b;
}
public static void main(String[] args) {
int a, b;
s = new Scanner(System.in);
System.out.println("Enter a value: ");
a = s.nextInt();
boolean input = false;
while (input == false) {
try {
System.out.println("Enter b value");
b = s.nextInt();
System.out.println("Sum of division is: " + division(a, b));
input = true;
}
catch (ArithmeticException e) {
System.err.println("Don't divide by zero!!!");
}
catch (InputMismatchException e) {
System.err.println("Enter just a Number!!!");
}
finally {
System.out.println();
}
}
}
}
You need to handle the erroneous input as well if you want the while loop to continue properly: You need to get rid of the erroneous input at the end of each catch block. Adding continue will simply make the loop run again until the user gives the correct input.
catch (InputMismatchException e)
{
System.err.println("Enter just a Number!!!");
//add this
s.nextLine();
continue;
}
The program takes user input which is supposed to be an integer greater than 0. If the user doesn't do this he is notified of the mistake and is reprompted. Once the correct input is entered, the value is returned. What's the best way to do this? The following code is my try but doesn't work. It seems unnecessarily complex for such an easy task.
System.out.println("Please enter an integer greater than 0:");
Scanner scan = new Scanner(System.in);
int red = -1;
do
{
try
{
red = scan.nextInt();
}catch(InputMismatchException e)
{
System.out.println("Number must be an integer");
scan.nextLine();
if(red < 1)
System.out.println("Number must be more than zero");
else
break;
}
}while(true);
return red;
Sometimes I don't know what to put in my question because I already know the code doesn't work - so if there's something else I should tell please let me know.
The basic concept is running in the right direction, beware though, nextInt won't consume the new line, leaving it within the scanner, meaning you will end up with an infinite loop after the first unsuccessful loop.
Personally, I would simply get the input as a String using nextLine, which will consume the new line, causing the next loop to stop at the statement.
Then I would simply parse the String to an int value using Integer.parseInt
For example...
Scanner scan = new Scanner(System.in);
int red = -1;
do {
System.out.print("Please enter an integer greater than 0:");
String text = scan.nextLine();
if (text != null && !text.isEmpty()) {
try {
red = Integer.parseInt(text);
// This is optional...
if (red < 1) {
System.out.println("Number must be more than zero");
}
} catch (NumberFormatException exp) {
// This is optional...
System.out.println("Not a number, try again...");
}
}
} while (red < 1);
I use this class instead of the Scanner or BufferedReader classes to get user input:
import java.io.*;
public class Input{
private static BufferedReader input=new BufferedReader
(new InputStreamReader(System.in));
public static Double getDouble(String prompt){
Double value;
System.out.print(prompt);
try{
value=Double.parseDouble(Input.input.readLine());
}
catch (Exception error){
// error condition
value=null;
}
return value;
}
public static Integer getInteger(String prompt){
Integer value;
System.out.print(prompt);
try{
value=Integer.parseInt(Input.input.readLine());
}
catch (Exception error){
// error condition
value=null;
}
return value;
}
public static String getString(String prompt){
String string;
System.out.print(prompt);
try{
string=Input.input.readLine();
}
catch (Exception error){
// error condition
string=null;
}
return string;
}
}
Now, to answer your question u can write your code like this:
public class InputTest {
public int checkValue() {
int value;
do {
value = Input.getInteger("Enter a value greater than 0: ");
} while (value <= 0);
return value;
}
}
import java.util.*;
public class ArrayExample {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
boolean done = false;
while (!done) {
try {
System.out.println("Please enter the size of the array:");
String input = keyboard.next();
int size = new Integer(input).intValue();
int numbers[] = new int[size];
for (int i = 0; i < 20; i++) {
numbers[i] = i;
done = true;
System.out.println("Good.");
}
} catch (NumberFormatException ex) {
System.out.println("NumberFormatException Error. Please enter a integer.");
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("ArrayIndexOutOfBoundsException Error. Please enter 20 or higher.");
} catch (NegativeArraySizeException ex) {
System.out.println("NegativeArraySizeException Error. Please do not enter a negative.");
}
}
}
}
When I run this program it does not function properly. It should throw a exception unless I enter a INTEGER 20 or higher. However, it prints "Good." if I enter a number lower then 20. So if I enter 19 it will print "Good." 19 times. If I enter 3 it will print "Good." 3 times. I only want it to print "Good." if I enter 20 or higher. If not it should continue to loop through the exceptions.
You don't need anArray and IndexOutOfBoundException
for (int i = 0; i < size; i++) // < --- use (size) instead of constant 20 value
{
System.out.println("Good.");
}
also add finally block with done = true;
}
finally
{
done = true;
}
This will prevent the infinite loop throwing an exception.
Also add validation at the beginning of the program:
int size = new Integer(input).intValue();
if (size >= 20)
{
throw new IllegalArgumentException("Number more then 19");
}
I didn't understand fully, but what I have got this program to do is if the number is less than 20, it will throw the ArrayIndexOutOfBoundsException, but if it is 20 or greater, it will print out "Good" for how many ever times the integer size was. I also changed the for loop into an enhanced for loop:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
boolean done = false;
while (!done) {
try {
System.out.println("Please enter the size of the array:");
String input = keyboard.next();
int size = new Integer(input).intValue();
int numbers[] = new int[size];
if(numbers.length >= 20) {
for (int i : numbers) {
numbers[i] = i;
done = true;
System.out.println("Good.");
}
} else {
throw new ArrayIndexOutOfBoundsException("ArrayIndexOutOfBoundsException Error. Please enter 20 or higher.");
}
} catch (NumberFormatException ex) {
System.out.println("NumberFormatException Error. Please enter a integer.");
} catch (NegativeArraySizeException ex) {
System.out.println("NegativeArraySizeException Error. Please do not enter a negative.");
}
}
}
try the below code, you need to check if number given number is less than 20 and throw the exception, that part of code is missing
import java.util.*;
public class ArrayExample {
private static Scanner keyboard;
public static void main(String[] args) throws Exception {
keyboard = new Scanner(System.in);
boolean done = false;
while (!done) {
try {
System.out.println("Please enter the size of the array:");
String input = keyboard.next();
int size = new Integer(input).intValue();
int numbers[] = new int[size];
if(size <20){
throw new Exception("Number less than 20");
}
for (int i = 0; i < 20; i++) {
numbers[i] = i;
done = true;
System.out.println("Good.");
}
} catch (NumberFormatException ex) {
System.out.println("NumberFormatException Error. Please enter a integer.");
} catch (ArrayIndexOutOfBoundsException ex) {
System.out.println("ArrayIndexOutOfBoundsException Error. Please enter 20 or higher.");
} catch (NegativeArraySizeException ex) {
System.out.println("NegativeArraySizeException Error. Please do not enter a negative.");
}
}
}
}
The easiest way to answer this is to just walk through the code and figure out exactly what is happening. For simplicity, let's say you enter three.
import java.util.*;
public class ArrayExample {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
boolean done = false; //ok so we set done to false
while (!done) { //first time through, we are not done, so we enter the loop
try {
System.out.println("Please enter the size of the array:"); //you ask for a number
String input = keyboard.next(); //we put in '3'
int size = new Integer(input).intValue(); //convert the input to an int
int numbers[] = new int[size]; //we set the size to '3'
for (int i = 0; i < 20; i++) { //we enter the loop, i is 0 so we continue
numbers[i] = i; //we set the value of idx 0 to 0
done = true; //we set done to true so we will exit the while loop
System.out.println("Good."); //we print good
//we increment i to 1
}
//EXPANDED LOOP FOR CLARITY
for (int i = 0; i < 20; i++) { //second time, i is now 1
numbers[i] = i; //we set the value of idx 1 to 1
done = true; //we set done to true again (kind of redundant)
System.out.println("Good."); //we print good
//we increment i to 2
}
for (int i = 0; i < 20; i++) { //third time, i is now 2
numbers[i] = i; //we set the value of idx 2 to 2
done = true; //we set done to true again (still kind of redundant)
System.out.println("Good."); //we print good
we increment i to 3
}
for (int i = 0; i < 20; i++) { //fourth time, i is now 3
numbers[i] = i; //at this point we should throw an ArrayIndexOutOfBoundsException, so go to that catch statement
done = true;
System.out.println("Good.");
}
} catch (NumberFormatException ex) {
System.out.println("NumberFormatException Error. Please enter a integer.");
} catch (ArrayIndexOutOfBoundsException ex) { //so here we catch the AIOB exception
System.out.println("ArrayIndexOutOfBoundsException Error. Please enter 20 or higher."); //we print out an error, then continue
} catch (NegativeArraySizeException ex) {
System.out.println("NegativeArraySizeException Error. Please do not enter a negative.");
}
//here at the end of the block, we go back to check the while condition
//we set done to true, so the while will fail and we will exit the program
}
}
}
Given that, your output should look like this:
Good
Good
Good
ArrayIndexOutOfBoundsException Error. Please enter 20 or higher.
And that's all. The program will be done. Because you haven't posted the output, it would be difficult to debug this further.
Add the following lines after int size = new Integer(input).intValue();
if(size<20)
throw new ArrayIndexOutOfBoundsException("size is not equal to or greater than 20");
It will throw an Exception if the Size is less than 20. And no need to write those try and catch methods.
Hope this helped you...
Hey there! I'm trying to do some data input validation but I haven't been able to figure it out. I'm getting an infinite while loop in when I try to validate if the first character entered is a letter. . . .
Thanks for your help!
public class methods
{
public static void main(String args[]) throws IOException
{
String input ="";
int qoh=0;
boolean error=true;
Scanner keyboard = new Scanner (System.in);
//while (error)
//{
//error=true;
while (error==true)
{
System.out.print("\nEnter Quantity on Hand: ");
input = keyboard.nextLine();
if (input.length() <1)
{
System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500");
error=true;
System.out.println(qoh);
System.out.println(input);
}
else
{
error=false;
}
}
error = true;
while (error==true)
{
if (Character.isLetter(input.charAt(0)))
{
System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500");
error=true;
System.out.println(qoh);
System.out.println(input);
}
else
{
qoh = Integer.parseInt(input);
error=false;
}
}
}
}
You don't have an input = keyboard.nextLine(); in your second while loop.
You could refactor your code to only ask for new input when there is an error. So right after the sysout of 'ERROR...'
Extra:
I would actually do this different. The 'error = true' at the beginning is a bit confusing, because there might not be an error.
You could for example write a method called tryProcessLine, which reads the input and returns true if ok and false if there was an error, and than just do something like while(!tryProcessLine()){ }
Working example below:
import java.io.IOException;
import java.util.Scanner;
public class Methods {
private static int qoh;
public static void main(String args[]) throws IOException {
while (!tryProcessLine()) {
System.out.println("error... Trying again");
}
System.out.println("succeeded! Result: " + qoh);
}
public static boolean tryProcessLine() {
String input = "";
Scanner keyboard = new Scanner(System.in);
System.out.print("\nEnter Quantity on Hand: ");
input = keyboard.nextLine();
try {
qoh = Integer.valueOf(input);
if (qoh < 0 || qoh > 500) {
System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500");
return false;
} else {
return true;
}
} catch (NumberFormatException e) {
System.out.println("\n**ERROR06** - Quantity on hand must be numeric");
return false;
}
}
}
The problem is in this section:
while (error==true)
{
if (Character.isLetter(input.charAt(0)))
{
System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500");
error=true;
System.out.println(qoh);
System.out.println(input);
}
else
{
qoh = Integer.parseInt(input);
error=false;
}
}
Once you have a letter in the first position, this loop can never terminate. It checks whether a letter is in the first position (it is), prints it, and repeats. Try changing to:
while (error==true)
{
if (Character.isLetter(input.charAt(0)))
{
System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500");
error=false;
...
Also, a couple of other things:
while (error == true) can be shortened to while(error).
Also, Integer.parseInt will throw a NumberFormatException if the input is not an integer - you need to catch and handle this.
Also, why do you need the second loop at all? It seems like it is only supposed to validate the input - if so, you can move this logic into the first loop and eliminate the second one. Only use loops for things that should happen repeatedly (like the user entering input data). There is no need to check the same input repeatedly.
The infinite loop occurs because the second while loop is repeatedly checking whether the first character in the String (input.charAt(0)) is a letter. Assuming that the result from this check is true the loop will never terminate.
Your code could be simplified to something like:
Integer qty = null;
while (scanner.hasNext() && qty == null) {
String line = scanner.next();
try {
qty = Integer.parseInt(line);
} catch(NumberFormatException ex) {
System.err.println("Warning: Ignored non-integer value: " + line);
}
}
if (qty == null) {
System.err.println("Warning: No quantity specified.");
}
If it is a character, you're allowing error to still = true, which is causing that loop to continue forever, you're not ever going back to the beginning and reading another line.
Here is some code that does what you want and is structured a little better.
public class ScanInfo {
Scanner keyboard = new Scanner(System.in);
public ScanInfo(){
String line = getLineFromConsole();
while(null != line && !"quit".equals(line)){
if(isValidInput(line)){
int validNumber = Integer.parseInt(line);
System.out.println("I recieved valid input: "+validNumber);
}else{
System.out.println("\n**ERROR06** - Quantity on hand must be between 0 and 500");
}
line = getLineFromConsole();
}
}
private boolean isValidInput(String line){
//basic sanity
if(null == line || line.length() < 1){
return false;
}
try {
int number = Integer.parseInt(line);
return (number >= 0 && number <= 500);
} catch (NumberFormatException e) {
return false;
}
}
public static void main(String[] args) {
new ScanInfo();
}
public String getLineFromConsole(){
System.out.print("\nEnter Quantity on Hand: ");
return keyboard.nextLine();
}
}