• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Wondering about this error message regarding a variable

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just have a question about an error message I got. Here's the code: it's while loop inside a for loop to determine the proper length of a variable:




Now, when the loop didn't work correctly, I did some fiddling and put it in the right place -- in an else statement right after the If statement. The question I have is this; the class wouldn't even compile when it was written as above. This is the error message I got:

thoroughbredtestdrive.java:30: cannot find symbol
symbol : variable name
location: class thoroughbredtestdrive
horse[i].setName(name);
^


I was just wondering what was going on here -- I've initialized the variable, so why do I get this message? (actually the carat was under the variable name inside the parentheses; don't know why it isn't here).
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your name variable is defined inside the while loop. Its scope is therefore limited to that loop.

When you attempt to access a variable outside its scope, it cannot be found. Hence the error.

You likely want to define the variable outside of the while loop, but set its value within.
 
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never write == true or == false. Not if (b == true)…, but if (b)… and not if (b == false)… but if (!b)… Not only is that construct poor style, but also very error‑prone. You might write = by mistake.
It looks very awkward programming to initialise that boolean before the loop and reset it inside. I think a do‑while loop might be a lot more elegant. Assign the loop variable in the last line of the loop; that way you won't have an uninitialised local variable.

Your indentation is inconsistent; we have some suggestions here. If you indent your code correctly, you can see its structure. You will then see that you are assigning that boolean outside the loop. Work out what will stop the loop repeating itself.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haven't gotten to do-while loops yet or the naming conventions for booleans that you've described; just following what I've seen in HFJ so far.

Assign the loop variable in the last line of the loop; that way you won't have an uninitialised local variable.



Could you give me an example? I'm not quite sure I follow this.

Plus: the reason why I put the string name in the while loop is so if it isn't the proper length, then it goes back and ask the user to reenter the name; if it is the proper name then it sets the name and goes on to the object in the array; if I take the name out of the loop this doesn't happen -- it just prints the error message and goes on to the next object.

 
Campbell Ritchie
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't tell you anything about naming conventions. I told you about a style problem and a potential logic error. If you write = by mistake and reuse the boolean, you might even have two errors for the price of one
You can read about the do loop here.
 
Campbell Ritchie
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't assign a local variable the compiler will complain. The way I wrote that example, the local variable is not used until the end of the loop, and you can see it is assigned to, one line before that.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much.


Never write == true or == false. Not if (b == true)…, but if (b)… and not if (b == false)… but if (!b)



I wasn't clear -- I only meant that i haven't come across this way of evaluating a boolean expression in the book, so I wasn't familiar with it; all I've seen so far is the (==) to test equality.

 
Campbell Ritchie
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can test equality; there are all sorts of things you can do like if (p == q)… or if (p != q)… where p and q are both booleans. But don't go testing equality with true or false. There is still a risk of writing = instead of == by mistake.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize to keep writing about this, but I'm not understanding something.

take a loot at this example from chap 2:




It's line 21; when I saw this I got confused -- I guess I don't understand it -- while what is true? or what condition is being tested that's true that causes the while loop to run? (Maybe the book wasn't as clear as it could be). I may not be expressing myself clearly enough either.
 
Campbell Ritchie
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have got some cases where you are using equality operators correctly, as I said earlier.
And true is always true; the construct while (true)... creates an infinite loop; it never stops unless something in the loop stops it, for example break;

I seem to recognise that code; is it from Head First Java?
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christopher Laurenzano wrote:It's line 21; when I saw this I got confused -- I guess I don't understand it -- while what is true?


The general form of a while loop is

Now, a boolean expression is something that evaluates to true or false, so
x == 1 is a boolean expression
6 < 9 is a boolean expression

true is also a boolean expression (because it evaluates to true), so your while loop is actually saying

And as true is always true, you have a potential infinite loop. The only way to exit the loop is with a break or return statement.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:The general form of a while loop is


However (and before Campbell wades in ), in Java that should always be coded as:
while (<boolean_expression>) { ...

Winston
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, it's from Head First. I guess what I wanted to say is that I haven't seen that way of coding a boolean condition in the book; only the == (or maybe they did and I just missed it.)
 
Campbell Ritchie
Marshal
Posts: 80618
469
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . (and before Campbell wades in ) . . .

You're a bit late for that.

I still disagree about the bit about == true.
b == true is in itself a boolean expression, so that would have to be b == true == true
And that is a boolean so it becomes b == true == true == true
Etc, etc.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Winston Gutkowski wrote: . . . (and before Campbell wades in ) . . .

You're a bit late for that.

I still disagree about the bit about == true.
b == true is in itself a boolean expression, so that would have to be b == true == true
And that is a boolean so it becomes b == true == true == true
Etc, etc.


I was being a bit lazy. What I should have written was
 
Campbell Ritchie
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with that
reply
    Bookmark Topic Watch Topic
  • New Topic