VERY n00b question

C

Cork Soaker

I doubt that my subject makes sense.

So, I am a complete n00b to Java and so, I have a book...

In the book is the following program:

import java.io.*;
class FlatBroke
{
public static void main(String[] args)
{
for (int i=10; i=0; i--)
{
System.out.println("Pounds left: £" +i);
} //end actions depending on counter
System.out.println("Flat Broke!");
} //end method
} //end class definition



Now, it certainly seems simple, and correct, but I get the following
when trying to compile:

$ javac FlatBroke.java
FlatBroke.java:6: incompatible types
found : int
required: boolean
for (int i=10; i=0; i--)
^
1 error



As I see it, I'm even using an old book, or the wrong version of Java?
I'm using the OpenJDK 6 from the Ubuntu Hardy repositories. I wouldn't
have thought the versions would be this much different, so I'm clearly
missing something and I'm too dumb to see what.

I can't see why it would need to be a boolean, but then, I'm pretty
clueless as you have probably guessed.

I'm sure an expert can instantly see my problem. Can anyone help? :)
I looked at some pages from Google but they just confused me!

TIA
 
L

Lew

Cork said:
import java.io.*;
class FlatBroke
{
         public static void main(String[] args)
         {
                 for (int i=10; i=0; i--)
                 {
                         System.out.println("Pounds left: £" +i);
                 } //end actions depending on counter
                 System.out.println("Flat Broke!");
         } //end method

} //end class definition

Now, it certainly seems simple, and correct,

Nope. The assigment in the condition instead of a comparison is very
wrong.
but I get the following when trying to compile:

$ javac FlatBroke.java
FlatBroke.java:6: incompatible types
found   : int
required: boolean
                for (int i=10; i=0; i--)
                                ^
1 error

Notice that the error message very precisely and explicitly shows the
exact location and nature of the error.
As I see it, I'm even using an old book, or the wrong version of Java?

Neither, you have a typo. Use >= instead of =.
I'm using the OpenJDK 6 from the Ubuntu Hardy repositories.  I wouldn't
have thought the versions would be this much different, so I'm clearly
missing something and I'm too dumb to see what.

You are missing what the error message tells you explicitly, that you
have an int expression where you need a boolean expression. It's an
int expression because = assigns values, it does not compare them.
I can't see why it would need to be a boolean, but then, I'm pretty

It needs to be a boolean because it is a condition, and the loop is
only entered when the condition is 'true'. Only a boolean can be
'true' or 'false'. An int cannot be.
 
A

Andreas Leitgeb

Cork Soaker said:
for (int i=10; i=0; i--)

If this line is in the book *exactly* as you presented
it here, then throw away the book, and buy a different one.
That line was never ever correct in any version of Java.
 
R

Roedy Green

for (int i=10; i=0; i--)

it is an error he might have meant to write:

for (int i=0; i>=0; i--)

You need a boolean expression in the middle.
If is true, you do the loop body.
If false, you are done.

i==0 would be an boolean, syntactically correct, but meaningless.
 
M

Mark Space

Roedy said:
it is an error he might have meant to write:

for (int i=0; i>=0; i--)

You need a boolean expression in the middle.
If is true, you do the loop body.
If false, you are done.

i==0 would be an boolean, syntactically correct, but meaningless.

Good catch. Looking at that loop again, i>0 might also be the intended
expression.
 
C

Cork Soaker

Eric said:
Cork said:
[...]
Now, it certainly seems simple, and correct, but I get the following
when trying to compile:

$ javac FlatBroke.java
FlatBroke.java:6: incompatible types
found : int
required: boolean
for (int i=10; i=0; i--)
^
1 error

As I see it, I'm even using an old book, or the wrong version of Java?
I'm using the OpenJDK 6 from the Ubuntu Hardy repositories. I
wouldn't have thought the versions would be this much different, so
I'm clearly missing something and I'm too dumb to see what.

Even a very old book should explain that the assignment
operator `=' and the equality comparison operator `==' are
not the same ... Even so, `==' is probably not what you want
here; `>' or `>=' or maybe `!=' would make more sense. Your
book, even if it's Ye Ancient Tome, should describe all of them.

That makes sense, but, no, the book does not say this. I have looked
over it and the code is correct (according to the book). This certainly
makes sense though (what you said).
Java requires a boolean expression in any true/false context,
like the innards of an `if(...)' or `while(...)'. Since the middle
piece of a `for(...)' is just such a true/false context (it finds
the answer to "Should I keep on going?"), you need some expression
that produces a boolean value, either `true' or `false'. `0' is
neither of these, and is not a boolean.

I understand that, but as you said, this book doesn't say that. I may
have got a crap book. :-/
 
C

Cork Soaker

Andreas said:
If this line is in the book *exactly* as you presented
it here, then throw away the book, and buy a different one.
That line was never ever correct in any version of Java.

That's what I suspected. Damn, waste of money!
Thanks. :)
 
C

Cork Soaker

rossum said:
I doubt that my subject makes sense.

So, I am a complete n00b to Java and so, I have a book...

In the book is the following program:

import java.io.*;
class FlatBroke
{
public static void main(String[] args)
{
for (int i=10; i=0; i--)
{
System.out.println("Pounds left: £" +i);
} //end actions depending on counter
System.out.println("Flat Broke!");
} //end method
} //end class definition



Now, it certainly seems simple, and correct, but I get the following
when trying to compile:

$ javac FlatBroke.java
FlatBroke.java:6: incompatible types
found : int
required: boolean
for (int i=10; i=0; i--)
^
1 error
Read the error message: "found : int" means that the compiler has
found an integer value at the point indicated by the '^', that is at
"i=0".

The error massage also says: "required : boolean". The compiler wants
a boolean here, not the int you have supplied it with.

Look carefully at the indicated piece of code: "i=0". What have you
done here that makes it an int instead of a boolean? What does the
'=' operator do? What similar operator gives a boolean result?

Always read the error message and try to figure out what it is telling
you.

As I see it, I'm even using an old book, or the wrong version of Java? Neither.

I'm using the OpenJDK 6 from the Ubuntu Hardy repositories. I wouldn't
have thought the versions would be this much different, so I'm clearly
missing something and I'm too dumb to see what.
Not dumb. You are just seeing it for the first time, so it is more
difficult to work out what is happening. When you have seen this
error for the 100th time you will see it almost instantly.
I can't see why it would need to be a boolean, but then, I'm pretty
clueless as you have probably guessed.
Read the decription of the for loop and what the purpose of the second
element (your "i=0") is.

rossum
I'm sure an expert can instantly see my problem. Can anyone help? :)
I looked at some pages from Google but they just confused me!

TIA


Thanks, yours and other have explained the error. The error *is* in the
code, but the code is provided by the book. I bought the wrong book. :-(
 
C

Cork Soaker

Mark said:
Good catch. Looking at that loop again, i>0 might also be the intended
expression.


Thanks guys. Now it's been explained, it makes sense. Sadly, this is
the code in the book, and it is wrong.

Damn, it looked like such a good book too!

I'm a little bit p*ss*d off now! Bloody stupid book! :p
 
M

Mark Space

Cork said:
Thanks guys. Now it's been explained, it makes sense. Sadly, this is
the code in the book, and it is wrong.

Damn, it looked like such a good book too!

I'm a little bit p*ss*d off now! Bloody stupid book! :p

Yup, seems like it's pages would be best used to line the cat box.

In the meantime, there are a few free resources. First, obviously there
is Sun's Java tutorial.

<http://java.sun.com/docs/books/tutorial/>

I also like Java Passion. It's a website run by a Sun researcher who
gives free, on-line classes on various aspects of Java, including basic
Java. I think the basic course is starting in two days. If you jump in,
you can get in on it. Actually you can join anytime, it just helps not
to have to catch up.

<http://www.javapassion.com/javaintro/>

If you want a good dead tree book, I recommend _Learning Java_ by
O'Reilly. It's a good introduction to Java, and it also has enough
examples and discussion of intermediate level stuff to act as a
reference for a long time. I still pull mine out when I want some info
on an aspect of Java I haven't dealt with in a while. Make sure to get
the latest edition, which is third.

<http://oreilly.com/catalog/9780596008734/>

Good luck.
 
R

Roedy Green

Thanks guys. Now it's been explained, it makes sense. Sadly, this is
the code in the book, and it is wrong.

Damn, it looked like such a good book too!

I'm a little bit p*ss*d off now! Bloody stupid book! :p

what's the book. It deserves a onion for that.
 
H

Hal Rosser

Cork Soaker said:
I doubt that my subject makes sense.

So, I am a complete n00b to Java and so, I have a book...

Please share with us - What is the title, author, ISBN of this book you are
using.
 
M

Mike Schilling

Cork said:
Thanks guys. Now it's been explained, it makes sense. Sadly, this
is
the code in the book, and it is wrong.

Damn, it looked like such a good book too!

Well, one typo doesn't mean it's complete crap. (It's not a good
sign, though ...) It may be worth looking on the web for a list of
errata.
 
C

Cork Soaker

Hal said:
Please share with us - What is the title, author, ISBN of this book you are
using.

Teach Yourself Java Second Edition by Chris Wright.
Hodder & Stoughton
ISBN 0-340-78229-3
Printed 2006 AFAICT.

I don't think there's any point in going on using this book.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top