LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2006, 02:24 AM   #1
Fredstar
Member
 
Registered: Jul 2004
Location: Rochester, NY
Distribution: Fedora9::FreeBSD7.1
Posts: 296

Rep: Reputation: 30
Checking numerical data!! (java)


Checking numerical data!!

I have to use JOptionPane for a project for school.

I need a quick way to check if the input stream is holding an integer before I parse the data into an integer variable.

I thought I could do this with an if statement but I was wrong, or so I think

Any pointers would help!!

thanks
 
Old 03-17-2006, 06:49 AM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
so you need to parse a string? you could maybe do something like this:
Code:
int foo; String bar;
try {
  foo = Integer.parseInt(bar);
} catch (NumberFormatException e) {
  // not an integer
}
 
Old 03-17-2006, 07:42 AM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
i usually use String::matches(String) .. it takes a regular expression as a string.. if you put it in your action handler then you can sort through input before you need an exception handled..

here is an example with a text box..
ex
Code:
if(textArea.getText().matches("[0-9]+"))
    System.out.println("good input");
else System.out.println("BAD input");
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Data Type Java Arrays InJesus Programming 3 01-07-2006 05:09 PM
C++: Saving numerical data from a file to an array geodpsa Programming 5 12-13-2005 02:31 PM
[java]removing unwanted data from a file djadhav Programming 1 10-24-2004 08:32 AM
JAVA:Problems in Displaying Data to Terminal.. nedian123 Programming 1 07-06-2004 10:13 PM
java data structures tunedLow Programming 4 12-17-2002 11:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration