Results 1 to 7 of 7
  1. #1
    Napkins is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default 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.


    Java 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();
    	}
    In this case a valid input would be a number. Ex: 20
    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?

  2. #2
    mastal is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    You can use regular expression or write one more method that checks if Double.parseDouble(s) throw NumberFormatException.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    Napkins is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Thank you very much! The try/catch method did in fact work. This is the final code I came up with.

    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);
    			}
    		}
    However, I have one last question which I hope will be a little simpler.

    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.

  5. #5
    CTheSky is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    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. :)

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Napkins View Post
    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?
    Use the showConfirmDialog method instead. It returns an int value which you can check and perform whatever your heart desires based upon that value.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Napkins View Post
    Thank you very much! The try/catch method did in fact work. This is the final code I came up with.

    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);
    			}
    		}
    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.

    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?
    Test what the option pane output is if the user presses cancel -- that will help you in this.

Similar Threads

  1. Homework help involving comparing Color objects to Strings
    By SergeantJoKer in forum New To Java
    Replies: 12
    Last Post: 09-25-2010, 07:13 AM
  2. Replies: 45
    Last Post: 05-25-2010, 09:32 PM
  3. Replies: 2
    Last Post: 12-08-2009, 09:33 PM
  4. Replies: 5
    Last Post: 02-25-2009, 08:14 PM
  5. Simple program involving military time
    By busdude in forum New To Java
    Replies: 4
    Last Post: 10-08-2008, 07:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •