• Post Reply Bookmark Topic Watch Topic
  • New Topic

while loop  RSS feed

 
abdel Surez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I just started a computer programming program in the college.
I have an issue to resolve my code using while loop.
can you help me please?

My assignment is:


The Greeting Machine's sole purpose is to greet users with the expression Hello <FirstName> <LastName>!! But, only after the user enters a code that only they know and their First and Last Name.

Develop a simple program that does the following:

1.Requests a secret code from the user (a code given by you to the user ;)) ◦Use of an if or switch is important here

2.Requests the user's First and Last Name into 2 separate variables ◦Use of 2 declared variables is important here

3.Displays a greeting to the user using their First and Last Name on one line (Hello <FirstName> <LastName>!!) ◦Use of System.out.format is important here

4.Return back to the next user in line and request again. Stop when the user selects the Enter key ◦Use of a loop and a condition statement is important here

my code right now is:




 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yeah, you need at least one while loop. The basic idea is that you're asking a certain question until you get a certain answer, at that point the loop stops cycling. In other words while you aren't getting the password to match, you keep asking for the password. Or, while the user does not want to exit the program, you keep asking for their first and last name.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is another question in beginning Java just like this, and here is my answer from there: Answer from Beginning Java
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Your code is difficult to read; I suspect it is because of incorrect indentation. We have some suggestions about indentation. Have a look at them. I shall see if I can correct your post, in which case it will look a lot better.
I can see another problem: too much in the main method. The ideal length for a main method is one statement. Everything else should be moved elsewhere.
If you enter the wrong password, your program will still ask for a name to be entered regardless.
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop when the user selects the Enter key

This requirement does not make any sense. You have to press the Enter key in order to get ANY input. Perhaps it means "the user enters an empty line" - i.e., presses only the enter key, without having entering any text at all ?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch.

A couple of tinny things from me:
1. Don't forget, class names suppose to start with an Upper case, so GreetingMachine would be desired name in your case.
2. Comments. Probably you have been asked to write them down. I want to nitpick. Line 11 "my first variable". That is not true, your first variable is on line 10, and it is called "myScanner". Anyway, what i'm trying to say, be super clear and accurate on comments. Also you used weird syntax, I never seen like that "// ... //". Comments usually looks like [1] /* ... */ or [2] // ... Try to think about the difference scenarios when one or another suppose to be used.
3. And comments should not repeat "obvious" things, as:
but instead they should tell why you did certain things in certain way, what impact it could have in specific scenarios. In short, it should describe things, which could not be that obvious from first look at code.

Best luck with assignment
 
abdel Surez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANK YOU SO MUCH FOR YOUR FEED BACK GUYS.
I JUST NEED TO KNOW IF MY WHILE LOOP SHOULD BE AFTER OR BEFORE THE CONDITION IF...

THANK YOU
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abdel Surez wrote:I JUST NEED TO KNOW IF MY WHILE LOOP SHOULD BE AFTER OR BEFORE THE CONDITION IF...


You probably didn't know this, but all-capitals signifies shouting on the Internet. So please don't shout at us.

As for your question: Neither. you should stop thinking about typing and start thinking about how your code should proceed. You already know, I hope, that you need a loop which runs until the person has entered the correct password. So when you check the person's password input (I'm assuming you meant that when you wrote "the condition if") you need to do that repeatedly until you have a correct password. So is that going to be before or after the loop? No, it isn't.
 
abdel Surez
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry I did not that....
thank you for letting me know.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read my previous post, it tells you. With example even.

abdel Surez wrote:THANK YOU SO MUCH FOR YOUR FEED BACK GUYS.
I JUST NEED TO KNOW IF MY WHILE LOOP SHOULD BE AFTER OR BEFORE THE CONDITION IF...

THANK YOU
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!