0% found this document useful (0 votes)
7 views2 pages

Input Validation for Integers and Alphabets

The document describes how to validate user input in Java to ensure only integers or alphabets are entered. For integers, it uses a do-while loop with a Scanner to get input and checks if it is an integer with hasNextInt(). For alphabets, it uses while and do-while loops with regular expressions to check if the input matches [a-zA-Z] and reprompts the user if not until valid input is received.

Uploaded by

aasxdas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Input Validation for Integers and Alphabets

The document describes how to validate user input in Java to ensure only integers or alphabets are entered. For integers, it uses a do-while loop with a Scanner to get input and checks if it is an integer with hasNextInt(). For alphabets, it uses while and do-while loops with regular expressions to check if the input matches [a-zA-Z] and reprompts the user if not until valid input is received.

Uploaded by

aasxdas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

To validate Only integers be entered

Scanner sc= new Scanner([Link]);


// scan the array
[Link]("plz enter values");
do {
if ([Link]()) {
number = [Link]();
isNumber=true;
}
else
{
[Link]("mistake");
isNumber=false;
[Link]();
}
} while (!(isNumber));

To validate Only alphabets be entered


a. Using while loop
Scanner sc= new Scanner([Link]);
// scan the array
name=[Link]();
while(![Link]("[a-zA-Z]"))
{
[Link]("retype") ;
name=[Link]();
}

b. Using Do while loop


do

{
[Link]("Enter String");
name=[Link]();
if ([Link]("[a-zA-Z]+"))
{
name = [Link]();
match=true;
}
else
{
[Link]("retype") ;
match=false;
}
}while(!match);

You might also like