Solved

Input Validation

Posted on 2002-04-08
9
214 Views
Last Modified: 2010-03-31
how to restrict the user to enter Integer only not float..
0
Comment
Question by:ms_lost
9 Comments
 
LVL 4

Expert Comment

by:pellep
ID: 6926487
'enter' in what context. Enter from commandline? Enter in a textfield?

You could do like this
(only an example)
// -->
        String sVal = "1.1123";
        try {
            Integer iVal = Integer.valueOf(sVal);
        } catch (NumberFormatException nfe) {
            System.out.println("ERROR. Input " + sVal + " not an integer!!!");
        }
// <--

sVal here represents your user-input and you put your specific errorhandling in the catch phrase.
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 6927148
mlost,

Can the user enter numbers like 123.0 or 0.0?
0
 

Author Comment

by:ms_lost
ID: 6927495
i tried it it's not working.. it says incompatible types Integer

System.out.println(" Enter the Mileage");
String sVal=Keyboard.readString();
try {
int mileage = int.valueOf(sVal);
} catch (NumberFormatException nfe) {
System.out.println("ERROR. Input " + sVal + " not an integer!!!");
            }
}

int mileage.is used as a parameters in sub classes.. plz try to answer as soon as possible
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 9

Expert Comment

by:Venci75
ID: 6927516
replace this:
int mileage = int.valueOf(sVal);
with this:
int mileage = Integer.parseInt(sVal);

and don't change anything
0
 

Author Comment

by:ms_lost
ID: 6927684
thanks alot..it worked perfect..but how to change the Error msg
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6927693
Is this what you want:
while (true) {
  System.out.println("Enter the Mileage");
  String sVal=Keyboard.readString();
  try {
    int mileage = int.valueOf(sVal);
    break;
  } catch (NumberFormatException nfe) {
    System.out.print("Not an integer. ");
  }
}

If not - be more specific
0
 
LVL 9

Expert Comment

by:Venci75
ID: 6927694
ops - sorry:

int mileage;
while (true) {
 System.out.println("Enter the Mileage");
 String sVal=Keyboard.readString();
 try {
   mileage = Integer.parseInt(sVal);
   break;
 } catch (NumberFormatException nfe) {
   System.out.print("Not an integer. ");
 }
}


0
 

Author Comment

by:ms_lost
ID: 6927708
ok thanks..can u plz explain for me the  :

while (ture)
try
catch(format....)


cuz they are new terms for me.. plz..and i'll add 45 points for that question
0
 
LVL 9

Accepted Solution

by:
Venci75 earned 95 total points
ID: 6927725
This loop will repeat the execution of its body uril the 'break' command. After the 'break' has been reached, the execution of your program will continue after the end of the while loop.

while (true) {
...
  break;
...
}

the Integer.parseInt() methos throws NumberFormatException exception to indicate that the supplied string paramter doesn't represent a number. If this exception is thrown - the execution will continue from the 'catch' section.

In other words - if the string is a correct integer - the break command will be executed and the 'while' loop will be exited. Otherwise - a NumberFormatException will be thrown and the execution will contntinue with the:
  System.out.print("Not an integer. ");
line and you won't exit the loop.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ArrayIndexOutOfBoundException 9 82
table example 4 29
Way to decrease size of apk file 9 69
Convert from a json string array to a Java object 3 32
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question