squirrology
Badges: 20
Rep:
?
#1
Report Thread starter 1 year ago
#1
How can I use a while loop to ensure that a String entered is bigger than 6 characters and if it is greater than 6 then it will prompt the user to re-enter the code? Specifically using the while loop and the length method.

This is my code so far:
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
String Name;
String confirm;
int greater = 6;
System.out.println("Please enter a Name:");
Name = sc.next(); sc.close();

{ while (greater > Name.length()); }

}
0
reply
winterscoming
Badges: 19
Rep:
?
#2
Report 1 year ago
#2
You're halfway there, The only real problem is just the structure of your while loop. Here's some examples from the Java docs:

https://docs.oracle.com/javase/tutor...lts/while.html

The docs refer to two different parts of a while loop, specifically the expression and the statement(s), and gives example syntax - look carefully at the positioning of the squiggly-brackets { and }
Code:
while (expression)
{
    statement(s);
}
The Important Stuff:
1) There is no semicolon ; at the end of while(expression) - this is important - don't put a semicolon here, because Java will not run the statements, it'll just keep on churning over and over in an infinite loop - your expression will be evaluated endlessly, but if the expression is just a simple boolean check, then it's value will never change!
2) the squiggly brackets{ and }go around the statement(s) to be repeated - they essentially show the "body" of the loop (i.e. where the behaviour begins and ends). - so your statements (i.e. the stuff to repeat) must go in-between those.
3) The 'while' keyword itself, and the expression go outside of the statement. You can think of the 'while' keyword and the 'expression' as being like the "header" of the loop - the while keyword is the loop's "flow control". (making the important decision on whether or not to run the loop before each iteration)

Breakdown:
- The expression is anything which yields a true or false - i.e. a condition. (It may be multiple conditions chained together. The expression is checked at the beginning of each iteration (i.e. before the while loop's body runs)

i.e. while (isSomethingTrue)

- The statement(s) are the things which happen, repeated "inside" the loop's body itself (inside the squiggly brackets { and }), each time the expression is evaluated to be true.
i.e.

Code:
{
    statement1();
    statement2();
    statement3();
    // etc . . . 
}
Your problem:
In your case, there are 3 parts to problem:
  1. Initially prompting the user for input
  2. Checking the input length
  3. prompting the user again for input if the length is wrong


consider this pseudocode:
Code:
PRINT ("Please Enter a Name:")
LET name = INPUT()

REPEAT WHILE LENGTH(name) IS GREATER THAN 6
BEGIN
    PRINT ("Name is too long, please re-enter a name: ")
    LET name = INPUT()
END
So back to the Java while structure:

Code:
while (expression)
{
    statement(s);
}
Firstly, you're starting out by inputting a name:

Code:
Scanner sc = new Scanner(System.in); 
System.out.println("Please enter a Name:"); 
String name = sc.next();
Secondly, you need to fill in the expression: - i.e. the logic which decides whether to execute the loop's body

Code:
while (name.length() > 6)
Lastly, you need the statement(s) inside the loop body (inside squiggly brackets{ and }) to input the name again if the while keyword decides to run the loop:
Code:
{
    System.out.println("Please enter a Name:");
    name = sc.next();
}
Does this make sense?
1
reply
squirrology
Badges: 20
Rep:
?
#3
Report Thread starter 1 year ago
#3
(Original post by winterscoming)
You're halfway there, The only real problem is just the structure of your while loop. Here's some examples from the Java docs:

https://docs.oracle.com/javase/tutor...lts/while.html

The docs refer to two different parts of a while loop, specifically the expression and the statement(s), and gives example syntax - look carefully at the positioning of the squiggly-brackets { and }
Code:
while (expression)
{
    statement(s);
}
The Important Stuff:
1) There is no semicolon ; at the end of while(expression) - this is important - don't put a semicolon here, because Java will not run the statements, it'll just keep on churning over and over in an infinite loop - your expression will be evaluated endlessly, but if the expression is just a simple boolean check, then it's value will never change!
2) the squiggly brackets{ and }go around the statement(s) to be repeated - they essentially show the "body" of the loop (i.e. where the behaviour begins and ends). - so your statements (i.e. the stuff to repeat) must go in-between those.
3) The 'while' keyword itself, and the expression go outside of the statement. You can think of the 'while' keyword and the 'expression' as being like the "header" of the loop - the while keyword is the loop's "flow control". (making the important decision on whether or not to run the loop before each iteration)

Breakdown:
- The expression is anything which yields a true or false - i.e. a condition. (It may be multiple conditions chained together. The expression is checked at the beginning of each iteration (i.e. before the while loop's body runs)

i.e. while (isSomethingTrue)

- The statement(s) are the things which happen, repeated "inside" the loop's body itself (inside the squiggly brackets { and }), each time the expression is evaluated to be true.
i.e.

Code:
{
    statement1();
    statement2();
    statement3();
    // etc . . . 
}
Your problem:
In your case, there are 3 parts to problem:
  1. Initially prompting the user for input
  2. Checking the input length
  3. prompting the user again for input if the length is wrong


consider this pseudocode:
Code:
PRINT ("Please Enter a Name:";)
LET name = INPUT()

REPEAT WHILE LENGTH(name) IS GREATER THAN 6
BEGIN
    PRINT ("Name is too long, please re-enter a name: ";)
    LET name = INPUT()
END
So back to the Java while structure:

Code:
while (expression)
{
    statement(s);
}
Firstly, you're starting out by inputting a name:

Code:
Scanner sc = new Scanner(System.in); 
System.out.println("Please enter a Name:";); 
String name = sc.next();
Secondly, you need to fill in the expression: - i.e. the logic which decides whether to execute the loop's body

Code:
while (name.length() > 6)
Lastly, you need the statement(s) inside the loop body (inside squiggly brackets{ and }) to input the name again if the while keyword decides to run the loop:
Code:
{
    System.out.println("Please enter a Name:";);
    name = sc.next();
}
Does this make sense?
Yes, it does make sense! Thank you so much for your help!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!! I wasn't even expecting a detailed response so THANK YOU! If I could give you 1000 reps I would.

Also, I didn't proof-read back my question/explanation, and when I was copying & pasting the code it must have messed up during the process and went "Pleaser a Name". When I went back to my IDE, it did actually say "Please enter a Name" :rofl:
0
reply
winterscoming
Badges: 19
Rep:
?
#4
Report 1 year ago
#4
(Original post by squirrology)
Yes, it does make sense! Thank you so much for your help!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!! I wasn't even expecting a detailed response so THANK YOU! If I could give you 1000 reps I would.

Also, I didn't proof-read back my question/explanation, and when I was copying & pasting the code it must have messed up during the process and went "Pleaser a Name". When I went back to my IDE, it did actually say "Please enter a Name" :rofl:
Glad it helped! Yeah pasting code into TSR is way more difficult than it should be! If I want to share anything more than a few lines of Java code on here, I usually paste into something like https://repl.it instead, and just share the link so that I don't have to rely on TSR [CODE] tags
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top

University open days

  • Regent's University London
    Postgraduate Open Evening Postgraduate
    Thu, 19 Sep '19
  • Durham University
    Pre-Application Open Days Undergraduate
    Fri, 20 Sep '19
  • Loughborough University
    Undergraduate Open Day Undergraduate
    Fri, 20 Sep '19

What's your favourite genre?

Rock (173)
24.3%
Pop (176)
24.72%
Jazz (29)
4.07%
Classical (41)
5.76%
Hip-Hop (128)
17.98%
Electronic (46)
6.46%
Indie (119)
16.71%

Watched Threads

View All