Solved

Input Validation

Posted on 2002-04-08
9
228 Views
Last Modified: 2010-03-31
how to restrict the user to enter Integer only not float..
0
Comment
Question by:ms_lost
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
ejb on wildfly 5 64
Java: The Public Class Main 4 66
What is the use of Forwarding Class in java 1 54
Java List 4 76
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

751 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