Welcome to Dream.In.Code
Become a Java Expert!

Join 149,492 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,319 people online right now. Registration is fast and FREE... Join Now!




Making sure input is only NUMBERS

 
Reply to this topicStart new topic

Making sure input is only NUMBERS

Rating  5
ejme
15 Mar, 2007 - 12:59 AM
Post #1

New D.I.C Head
*

Joined: 15 Mar, 2007
Posts: 2


My Contributions
How do you check input to make sure it only has numbers?

Or is there a way to make input compulsory numbers only?

eg. input is a salary:

CODE

System.out.print("Enter your annual salary: ");
salary = console.nextDouble();

User is offlineProfile CardPM
+Quote Post

Programmist
RE: Making Sure Input Is Only NUMBERS
15 Mar, 2007 - 02:16 AM
Post #2

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(ejme @ 15 Mar, 2007 - 01:59 AM) *

How do you check input to make sure it only has numbers?

Or is there a way to make input compulsory numbers only?

eg. input is a salary:

CODE

System.out.print("Enter your annual salary: ");
salary = console.nextDouble();



Inside a try-catch statement, use Double.parseDouble( String s ) to attempt to convert the input to a Double. if you catch a NumberFormatException then you know it was a bad input. Put all of this (including your prompt and input reading) inside a loop that only terminates if an exception is not thrown.
User is offlineProfile CardPM
+Quote Post

horace
RE: Making Sure Input Is Only NUMBERS
18 Mar, 2007 - 08:03 AM
Post #3

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 5 times
Dream Kudos: 50
My Contributions
you can test if the next token is a double, if not discard it. Keep doing that until you get a double entered, e.g.
CODE

System.out.print("Enter your annual salary: ");
// if next token is a double read it otherwise ignore it
while (!console.hasNextDouble()) console.next();    // not double, discard token
salary = console.nextDouble();


User is offlineProfile CardPM
+Quote Post

aRTx
RE: Making Sure Input Is Only NUMBERS
18 Mar, 2007 - 12:22 PM
Post #4

New D.I.C Head
*

Joined: 2 Dec, 2006
Posts: 2


My Contributions
if we have interactive input p.s

import javax.swing.*;
public class isNumber{
public static void main(String[] args){
String s1 = JOptionPane.showInputDialog("write something");

//we can convert this string to integer

int nr = Integer.parseInt(s1);

// but if the string does not content number the program will be fail

// I have a question, how to repeat input while we will write number

// plz tell us

System.out.println(nr);

}
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 05:43PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month