Solved

Input Validation

Posted on 2002-04-08
9
222 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

830 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