• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

While Loop with Sentinel issues

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello fellow ranchers,

I have been racking my brain on trying to figure out how to write the code for a while loop with a sentinel value which calls a variable (actually I think it will call a get method) from another class. I was able to get it to work when I had the variable in the same class, but when I handed in the assignment my instructor advised that I shouldn't declare the variables in the main program and they should all be in the employee.java file. He also stated that the while loops in my set methods shouldn't be there and be put into the main as well.

So I modified the program and I moved the while loops out of the set methods and I ended up using a while (true) look to get the sentinel value to work. I have read that using the while (true) and calling a break is not best practices so I am not satisfied with the program.

I have turned it in this way, but for my learning purposes, I would like to figure out how to use the while loop like I did in my first submission and not using the while(true). I think that it's an issue of getting the input.nextLine() into the loop somehow so that it will "listen" for the value that is being entered in the console. But every which way I tried, the program gave compile errors. I don't have all the "tries" and the errors... I would have to recreate them if anyone wanted to see that...

Can someone take a look and let me know what is needed in my code to get it to work like my first submitted assignment?

So... here is all the code for what I submitted for my assignment.

This is from my first submitted assignment:




And this is from the revised submission of the assignment with the while (true) loop:



 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To make things a bit clearer (hopefully), it is these lines that I would like to get working in the rework of the program where I used the while (true):

 
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have any problems with your revised code. What's not working for you? Errors? Incorrect output?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a lot of opinions on this, so I'll just tell you mine.

Generally, I like to use do/while loops if I'm going to do something at least once. This will avoid the "printing the prompt twice" issue you have.

Second, I wouldn't set the employee name when I know it's incorrect. So create some kind of input variable to test.

Now, if you have the outer do/while loop and an input variable, what ends the loop? I think you know.

There are two camps on the use of break. One says never use it. I think that's a bit extreme. I wouldn't have a problem with this:



This is just an example. You can do it other ways too.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I don't have any problems with your revised code. What's not working for you? Errors? Incorrect output?


Nothing is necessarily wrong with the revised code. It works as expected. I was just wondering if there is a better way to write it.
 
Marshal
Posts: 8853
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that assignment marking grids has line "is code clearly commented...", but that is the way too much.
You did good on choosing very clear class, methods, variables names and it is more important than comments, especially when they don't say anything new than code itself.
In practice never comment each line of code - it makes code unreadable.
 
Marshal
Posts: 28145
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amie Mac wrote:Nothing is necessarily wrong with the revised code. It works as expected. I was just wondering if there is a better way to write it.



When I was a beginner I thought that form of while-loop with the side-effect of assigning a String to a variable was kind of icky. But it's a common idiom and people do get used to it once they've seen it a few times. So I would leave it that way.
 
Marshal
Posts: 79076
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote: . . . that form of while-loop with the side-effect of assigning a String to a variable . . .

Agree with Paul C: that is a common idiom. But it does read strangely when you first see it, and you would never guess it if nobody showed you how to do it.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:There are a lot of opinions on this, so I'll just tell you mine.

Generally, I like to use do/while loops if I'm going to do something at least once. This will avoid the "printing the prompt twice" issue you have.

Second, I wouldn't set the employee name when I know it's incorrect. So create some kind of input variable to test.

Now, if you have the outer do/while loop and an input variable, what ends the loop? I think you know.

There are two camps on the use of break. One says never use it. I think that's a bit extreme. I wouldn't have a problem with this:



This is just an example. You can do it other ways too.



Yes, I like this do... while loop better than the while (true) loop that I have in my code. I will have to try it out this way at some point. Thanks for your input.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:I know that assignment marking grids has line "is code clearly commented...", but that is the way too much.
You did good on choosing very clear class, methods, variables names and it is more important than comments, especially when they don't say anything new than code itself.
In practice never comment each line of code - it makes code unreadable.



Yes, I don't like all the comments in the code either, because it does make it harder to read... I just wanted to make sure it was clearly commented.
Thanks for the tip!
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Paul Clapham wrote: . . . that form of while-loop with the side-effect of assigning a String to a variable . . .

Agree with Paul C: that is a common idiom. But it does read strangely when you first see it, and you would never guess it if nobody showed you how to do it.



Thank you Paul and Campbell... I am glad that there are places out there like Code Ranch that can assist with code writing since the textbooks don't have all the examples.
 
Campbell Ritchie
Marshal
Posts: 79076
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome

By the way: do you understand the syntax of that loop and why there have to ybe certain () in certain places?
A style thing: you should not usually have those spaces after ( or before ). We have some style suggestions of our own.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You're welcome

By the way: do you understand the syntax of that loop and why there have to ybe certain () in certain places?


Do you mean the () in this part of the loop code?

Do the () mean that it is a method and arguments or parameters can be passed to the method?

A style thing: you should not usually have those spaces after ( or before ). We have some style suggestions of our own.


Thank you. I read through these style suggestions and in there it is suggested to not use do... while loops.

 
Campbell Ritchie
Marshal
Posts: 79076
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got that bit right, but I actually meant the next pair of () outside that.
[Edit]Also, I meant in the line beginning while. Look in your first post, Payroll class, line 30.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You got that bit right, but I actually meant the next pair of () outside that.
[Edit]Also, I meant in the line beginning while. Look in your first post, Payroll class, line 30.



Do you mean this line?

I think that we have 3 methods here:
while()
input.nextLine()
equalsIgnoreCase()

but I am not sure why there are () around firstName = input.nextLine(). Is it because of the "!" ?
 
Carey Brown
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amie Mac wrote:
I think that we have 3 methods here:
while()
input.nextLine()
equalsIgnoreCase()

but I am not sure why there are () around firstName = input.nextLine(). Is it because of the "!" ?


You need those () because the assignment (=) operator has a lower precedence and you want the assignment to take place before the call to equalsIgnoreCase().
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Amie Mac wrote:
I think that we have 3 methods here:
while()
input.nextLine()
equalsIgnoreCase()

but I am not sure why there are () around firstName = input.nextLine(). Is it because of the "!" ?


You need those () because the assignment (=) operator has a lower precedence and you want the assignment to take place before the call to equalsIgnoreCase().



Ahh... yes. That makes sense.
Thanks
 
Campbell Ritchie
Marshal
Posts: 79076
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was what I wanted to know that you understood.
 
Liutauras Vilda
Marshal
Posts: 8853
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I found this page (<- link) most easy readable about operator precedence.
So you could find all of them there.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:And I found this page (<- link) most easy readable about operator precedence.
So you could find all of them there.



Thanks.

I didn't really think about that assignment being an operator and needing precedence.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic