Q:

Java program to validate input as integer value only

belongs to collection: Java Basic Programs

0

Java program to validate input as integer value only

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program

import java.util.Scanner;
import java.util.InputMismatchException;
public class Scan
{   
	// create function readint for reading input value.
	public static int readInt(String msg)
	{ 
		boolean error=false;
		int x=0;
		do
		{
			try
			{
				// create object of scanner class.
				Scanner KB=new Scanner(System.in);

				// enter here.
				System.out.print("Enter integer : ");
				x=KB.nextInt();
				error=false;
			}
			catch(InputMismatchException e)
			{
				// accept integer only.
				System.out.println("Invalid Input..Pls Input Integer Only..");
				error=true;
			}
		}
		while(error);
		return(x);
	}
}

Output

Enter integer : Good
Invalid Input..Pls Input Integer Only..
Enter integer : 10
Enter integer : 10

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to print spiral pattern of the given ... >>
<< Java program to calculate compound interest...