Results 1 to 17 of 17
  1. #1
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Help with IF statement

    I want to say If length is less than 5, display an error message and then ask for the number again. What my code is doing, is displaying an error message and then continuing to cycle thro the code as if there was no issue. Where did I err?
    Java Code:
    	if (String.valueOf(numOne).trim().length() != 5)
    
    		JOptionPane.showMessageDialog(null, "Please add valid input");

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Help with IF statement

    I want to say If length is less than 5
    Than why is the code written as 'if length is not equal to 5'?

  3. #3
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Help with IF statement

    Good catch....I want to say if the length does not equal 5.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Help with IF statement

    What my code is doing, is displaying an error message and then continuing to cycle thro the code as if there was no issue.
    The if is just that...it will execute the code following the statement if the condition is true - then continue onwards. Perhaps you wish to use an if/else?

  5. #5
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Help with IF statement

    I modified the code to an if else
    Java Code:
    		if (String.valueOf(numOne).trim().length() != 5)
    	
    			JOptionPane.showMessageDialog(null, "Please add valid input");
    		
    			else
    
    				StepTwo;
    What happens here is if the number entered does not equal 5 it displays the error, but it jumps on to the "else." I want it to cycle back up and continue to display the message "Enter a valid..." Until 5 digits have been entered.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Help with IF statement

    I don't understand the phrase 'jumps on to the else'...and you did not provide code for 'enter a value'...
    Suggested reading:
    The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    And an example of proper syntax:
    Java Code:
    if (some condition){
       //do something
    }else{
       //do something else
    }
    If you wish this to continue asking until a valid value is entered, have this if/else inside a while that evaluates a boolean flag (which is set to true in the else)

  7. #7
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Help with IF statement

    Quote Originally Posted by doWhile View Post
    I don't understand the phrase 'jumps on to the else'...
    What I was meaning there was that if I entered the number 2. I would get the message "Please add valid input", but instead of looping back up to my if statement and forcing the user to input a valid number, the code would move to the else, and then carry onto stepTwo. (And after reading your post, I need to use a while statement to make this happen).

  8. #8
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Help with IF statement

    I looked at the link you provided, and went to the while and do-while statement, and came up with this code:
    Java Code:
    while (String.valueOf(numOne).trim().length() != 5)
    
    	numOne = Integer.valueOf(JOptionPane.showInputDialog("Enter your input"));
    
    		if (String.valueOf(numOne).trim().length() != 5)
    
    			JOptionPane.showMessageDialog(null, "Please add valid input");
    		
    			else
    
    				StepTwo;
    This will continue to show the "Enter your input" message until valid input is entered, but is there a way to show the "Please add valid input" each time an invalid input is entered?

  9. #9
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Help with IF statement

    but is there a way to show the "Please add valid input" each time an invalid input is entered?
    The logic of the pseudo-code you posted does not do this?

  10. #10
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Help with IF statement

    The code I posted in my post # 8 does not do this. If the number entered is not equal to 5, it asks "Please Enter A 5 digit number"

  11. #11
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Help with IF statement

    Quote Originally Posted by Rahim2312 View Post
    The code I posted in my post # 8 does not do this. If the number entered is not equal to 5, it asks "Please Enter A 5 digit number"
    For future reference, I'd recommend describing the requirements in more detail - we don't know how you want your program to behave up front.

    That being said, have a look at my username...hint, hint

  12. #12
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Help with IF statement

    Sorry...I thought I was explaining well. I will try to explain more next time. Look at your username...so I would need a Do-While loop. I'm not following how to set - up a Do-While loop from the site....

    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

  13. #13
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Help with IF statement

    I still don't know how you wish it to behave, but writing things out in pseudo-code helps everyone:
    Java Code:
    do{
       //get input
       if ( invalid input ){
          //notify user of invalid input
       }else{
           //do something
       }
    }while (invalid input);
    Think about how you wish it to behave, step by step. Write it out on paper if you have to, similar to the above. Each logic you write on paper should have a way to write in code, which will help you translate to code.

  14. #14
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Help with IF statement

    Thank you for the help! I made that far more difficult than it should have been!!! I coded it wrong and as soon as I executed the program I would only get the prompt Please add valid input, and that's as far as it got!

    However, now it is working perfectly! Thank you for the patience and assistance.

  15. #15
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: Help with IF statement

    i was thinking about do...while and thinking: "why you guys don't use "do...while" ?" but at least, in the end, it has been chosen (at last...) :D but, anyway, just for an addition that if you want to do something about input e.g until users entered the correct (or desired input), do...while is one of the powerful method, another two can be useful, but use a little trick:

    Java Code:
    //while
    String users_input; //String users_input = ""; is this valid command in java?
    
    while (true) {
    	//prompt for the user's input
    	//...
    
    	if (String.valueOf(numOne).trim().length() != 5)
    		JOptionPane.showMessageDialog(null, "Please add valid input");
    	else {
    		JOptionPane.showMessageDialog(null, "OK");
    		break;
    	}
    }
    Java Code:
    //for
    String users_input;
    
    for ( ; ; ) { //infinite loop, just like "while"
    	//prompt for the user's input
    	//...
    
    	if (String.valueOf(numOne).trim().length() != 5)
    		JOptionPane.showMessageDialog(null, "Please add valid input");
    	else {
    		JOptionPane.showMessageDialog(null, "OK");
    		break;
    	}
    }

  16. #16
    Rahim2312 is offline Member
    Join Date
    Apr 2012
    Posts
    88
    Rep Power
    0

    Default Re: Help with IF statement

    Quote Originally Posted by chipp View Post
    Java Code:
    //for
    String users_input;
    
    for ( ; ; ) { //infinite loop, just like "while"
    	//prompt for the user's input
    	//...
    
    	if (String.valueOf(numOne).trim().length() != 5)
    		JOptionPane.showMessageDialog(null, "Please add valid input");
    	else {
    		JOptionPane.showMessageDialog(null, "OK");
    		break;
    	}
    }
    I guess I could have quoted either one of your code snippets, but what does the "break" statement do?

  17. #17
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  2. If Statement in SQL
    By Steffi1013 in forum JDBC
    Replies: 6
    Last Post: 04-10-2010, 03:19 PM
  3. need help in sql statement
    By chyeeqi in forum JDBC
    Replies: 6
    Last Post: 03-28-2010, 07:49 PM
  4. if statement help please!!
    By soc86 in forum New To Java
    Replies: 5
    Last Post: 12-02-2008, 02:56 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 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
  •