Solved

Input Validation

Posted on 2002-04-08
9
205 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now