0

so I am using a while loop to check if the string entered by a user is an empty string, and if it is it results in an error, and then asks a question again and if it is valid does not repeat. I got this, but how would I also check for a string longer then 60 characers that does the same as as the empty string and also check to see if the string ends in a question mark. As in how would I implement the multiple checks within the loop

while ( x.length() == 0||x.length >100){
            System.out.println("empty string not allowed .");
        	System.out.print( "ask another question: " );
        	x = scan.nextLine();
  
  
  }

So I have a loop which checks multiple conditions where I have the error message for empty string and this prints out if the string entered is empty, greater than 100 characters, or doesn't end in question mark. it will continue through until a valid question is input. How would I implement the error messages for just if the empty string is entered or a string that is greater than 100 or a string that doesnt end with a question mark

1
  • Include an And condition in while check, (x.length() == 0 || x.lenght()>60) Commented Oct 23, 2015 at 4:05

2 Answers 2

1

you can use 'or' in the condition checking.

while ( x.length() == 0 || x.length<60 || [Any other condition comes here]){
        System.out.println("empty string not allowed .");
        System.out.print( "ask another question: " );
        x = scan.nextLine();

Hope this answers your question.

Sign up to request clarification or add additional context in comments.

3 Comments

ok I edited the code a little is now also checking if the string entered is greater than 100 characters how would I print the error message just for that. so If the problem is an empty String, report that the String is empty -- if the problem is that the String is too long, report that the String is too long.
use a switch case in the while loop
You simply implement if, if else, else. while(conditions...){ if(x.lenght()==0){System.out.println("Empyt string not allowed");}else if([next condition here]){[print the output]}....else{[print the output]} x =scan.nextLine();
0

This should solve your purpose

while( x.trim().length()==0 || x.length()>60 || !x.endsWith("?") 
||(x.length().trim()==1 && x.endsWith("?")) ) {
   System.out.println("empty string not allowed .");
   System.out.print( "ask another question: " );
   x = scan.nextLine();
}

The last condition is to check if the user just enters a "?", then loop again so as to make him ask a question with content. So in effect this loops runs if :
The user enters empty string
The user enters more than 60 characters
The user does not end the question with '?'
The user just enters '?' without any content

4 Comments

I understand the conditions I am having trouble implementing the error message for the conditions so If the problem is an empty String, report that the String is empty is not allowed and if the problem is that the String is too long, report that the String is too long.
So simple use if conditions within the loop and print out the appropriate error within the if block if the condition evaluates to true. Also if all conditions are false ( meaning that the user has given valid input) then you can break out of the loop either by setting a boolean value to false (this boolean value is the loop condition) or just by using break keyword. Also make sure that each of your if blocks have a continue statement.
figured it out now doing a nested loop with a while loop where the user is asked a question and if they enter three different forms of yes it continues. What I amn having trouble with is exiting the program with if they enter anything else
If they enter anything else you can either go back to the beginning of the while loop through the use of continue or each if can have a inifinite while loop that continues until the user does not input valid content for that particular if block. To exit the program at any place you can just call System.exit(0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.