Results 1 to 7 of 7
- 02-01-2011, 10:52 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Simple question involving Strings and JOptionPane
As the forum suggests, I'm new to Java.
Essentially what I'm trying to do is create an if statement that will restart the class if a value entered into a JOptionPane.showInputDialog box isn't a valid entry.
In this case a valid input would be a number. Ex: 20Java Code:public void input() { String s = JOptionPane.showInputDialog("Input a value"); [B]if (s ?)[/B] { JOptionPane.showMessageDialog(null, "Error: You must enter a valid value.", "Error", JOptionPane.ERROR_MESSAGE); input(); }
An invalid input would be anything involving characters. Ex: Twenty
Also an invalid input would be leaving the input dialog box empty
So how should I structure the if argument to do this?
- 02-01-2011, 11:01 PM #2
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
You can use regular expression or write one more method that checks if Double.parseDouble(s) throw NumberFormatException.
-
Get your String, trim it by calling trim() on it, and then parse it into an int using Integer.parseInt(s). You should do this inside of a try/catch(NumberFormatException). If the parse succeeds, then code below the parse will be called. If not, the catch block will be called. If this doesn't make sense, please check out the Java Tutorials on how to use Exceptions.
- 02-02-2011, 01:23 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Thank you very much! The try/catch method did in fact work. This is the final code I came up with.
However, I have one last question which I hope will be a little simpler.Java Code:public void input() { String s = JOptionPane.showInputDialog("Input a value", "Input"); double value = 0; while (s != null) { try { value = Double.parseDouble(s); break; } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Error: You must enter a valid value.", "Error", JOptionPane.ERROR_MESSAGE); s = null; input(); System.exit(0); } }
With the JOptionPane.showInputDialog, at the bottom of the dialog box are the options OK and CANCEL, I was curious what statement could I use to end the program if they choose the option cancel?
The reason is that after the initial input I have another dialog box that appears that displays some manipulation of the "value" variable, and it still appears if I click cancel in the input dialog box, whereas I'd prefer the program end if someone clicks cancel, rather than just closing the dialog box.
- 02-02-2011, 01:47 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 4
- Rep Power
- 0
Hi,
You can create another method to check whether your inputed String is int and double or not.
For example:
public boolean isNum(String str)
{
String charSet = "0123456789.";
for(int i = 0; i < str.length; i++)
{
if(charSet.indexOf(str.substr(i,1))<0)
{
return false;
}
return true;
}
So, later in your code you already write, you may can do write this way:
public void input()
{
String s = JOptionPane.showInputDialog("Input a value");
if (! isNum(s) )
{
JOptionPane.showMessageDialog(null, "Error: You must enter a valid value.",
"Error", JOptionPane.ERROR_MESSAGE);
input();
}
That is my suggestion. Thanks. :)
- 02-02-2011, 02:26 AM #6
-
I wouldn't use recursion in this situation (calling a method from within the same method). Rather use a while loop, and change the boolean condition after the Double.parseDouble(..) call. This way the boolean will only change if the String parses successfully.
Test what the option pane output is if the user presses cancel -- that will help you in this.With the JOptionPane.showInputDialog, at the bottom of the dialog box are the options OK and CANCEL, I was curious what statement could I use to end the program if they choose the option cancel?
Similar Threads
-
Homework help involving comparing Color objects to Strings
By SergeantJoKer in forum New To JavaReplies: 12Last Post: 09-25-2010, 06:13 AM -
Question involving Polymorphism, inherticance, casting...
By myst in forum New To JavaReplies: 45Last Post: 05-25-2010, 08:32 PM -
Dealing with exceptions in my simple GUI app involving a process
By fawkes711 in forum AWT / SwingReplies: 2Last Post: 12-08-2009, 08:33 PM -
Need help with a simple Java thing involving array of objects
By Jeremy8 in forum New To JavaReplies: 5Last Post: 02-25-2009, 07:14 PM -
Simple program involving military time
By busdude in forum New To JavaReplies: 4Last Post: 10-08-2008, 06:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks