Solved

Input Validation

Posted on 2002-04-08
9
192 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

758 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

19 Experts available now in Live!

Get 1:1 Help Now