All things are lawful, but not all things are profitable.
Carey Brown wrote:I don't have any problems with your revised code. What's not working for you? Errors? Incorrect output?
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.
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.Paul Clapham wrote: . . . that form of while-loop with the side-effect of assigning a String to a variable . . .
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.
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.
Campbell Ritchie wrote:
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.Paul Clapham wrote: . . . that form of while-loop with the side-effect of assigning a String to a variable . . .
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?
A style thing: you should not usually have those spaces after ( or before ). We have some style suggestions of our own.
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.
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 "!" ?
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().
Liutauras Vilda wrote:And I found this page (<- link) most easy readable about operator precedence.
So you could find all of them there.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |