boolean statement

K

K

Why does my boolean statement have an error message next to it in eclipse?

public class boolean1 {

public static void main(String[] args) {
int gumballs;
int kids;
int gumballsPerKid;
boolean eachKidGetsTen;

gumballs = 140;
kids = 15;
gumballsPerKid = gumballs / kids;

System.out.print("True of False? ");
System.out.println("Each kid gets 10 gumballs");
eachKidGetsTen = gumballsPerKid >= 10;
System.out.println("eachKidGetsTen");

}

}
 
G

glen herrmannsfeldt

K said:
Why does my boolean statement have an error message next to it in eclipse?

It should be a warning, not an error, but you never use the value
of the variable. Eclipse nicely tells you that you might have
forgotten something.

-- glen
public class boolean1 {
public static void main(String[] args) {
int gumballs;
int kids;
int gumballsPerKid;
boolean eachKidGetsTen;

gumballs = 140;
kids = 15;
gumballsPerKid = gumballs / kids;

System.out.print("True of False? ");
System.out.println("Each kid gets 10 gumballs");
eachKidGetsTen = gumballsPerKid >= 10;
System.out.println("eachKidGetsTen");
 
E

Eric Sosman

Why does my boolean statement have an error message next to it in eclipse?

Probably because the variable `eachKidGetsTen' is not used.
It is given a value, but that value is never therafter consulted.

(For future reference: When you have a question about an
"error message," it is a good idea to quote the exact text of
the message. In this instance I imagine there was no text at
all, but still: Some description of the "error message" would
have been a good idea. As it is, I'm just guessing -- And, as
SH taught us, "It is a capital mistake to theorize before you
have all the evidence." But since I theorize ante-factually
only because you force me to, the offense is not mine but yours.
Off with your head!)
public class boolean1 {

public static void main(String[] args) {
int gumballs;
int kids;
int gumballsPerKid;
boolean eachKidGetsTen;

gumballs = 140;
kids = 15;
gumballsPerKid = gumballs / kids;

System.out.print("True of False? ");
System.out.println("Each kid gets 10 gumballs");
eachKidGetsTen = gumballsPerKid >= 10;
System.out.println("eachKidGetsTen");

If you removed the " marks, I bet it would pacify Eclipse.
 
K

K

K said:
Why does my boolean statement have an error message next to it in eclipse?



It should be a warning, not an error, but you never use the value

of the variable. Eclipse nicely tells you that you might have

forgotten something.



-- glen


public class boolean1 {


public static void main(String[] args) {
int gumballs;
int kids;
int gumballsPerKid;
boolean eachKidGetsTen;

gumballs = 140;
kids = 15;
gumballsPerKid = gumballs / kids;

System.out.print("True of False? ");
System.out.println("Each kid gets 10 gumballs");
eachKidGetsTen = gumballsPerKid >= 10;



}

what do you mean? How do you use the value of the variable? how would I fix my code?
 
G

Gene Wirchenko

Probably because the variable `eachKidGetsTen' is not used.
It is given a value, but that value is never therafter consulted.

(Your print statement does not refer to it.)
(For future reference: When you have a question about an
"error message," it is a good idea to quote the exact text of
the message. In this instance I imagine there was no text at
all, but still: Some description of the "error message" would
have been a good idea. As it is, I'm just guessing -- And, as
SH taught us, "It is a capital mistake to theorize before you
have all the evidence." But since I theorize ante-factually
only because you force me to, the offense is not mine but yours.
Off with your head!)

What he said.
public class boolean1 {

public static void main(String[] args) {
int gumballs;
int kids;
int gumballsPerKid;
boolean eachKidGetsTen;

gumballs = 140;
kids = 15;
gumballsPerKid = gumballs / kids;

System.out.print("True of False? ");
System.out.println("Each kid gets 10 gumballs");
eachKidGetsTen = gumballsPerKid >= 10;
System.out.println("eachKidGetsTen");

If you removed the " marks, I bet it would pacify Eclipse.

Specifically,
eachKidGetsTen
and
"eachKidGetsTen"
are two very different things that just happen to resemble each other
textually.

Sincerely,

Gene Wirchenko
 
A

Andreas Leitgeb

K said:
what do you mean? How do you use the value of the variable?
how would I fix my code?

Since it's only a warning, you can just run the code and see the
output. My guess is, you'll instantly spot the bug, and smile as
the warning will disappear as a consequence of fixing the bug.
 
L

Lew

Roedy said:
K wrote, quoted or indirectly quoted someone who said :
public class boolean1 {

public static void main(String[] args) {
int gumballs;
int kids;
int gumballsPerKid;
boolean eachKidGetsTen;

Classes should start with a capital letter. See
http://mindprod.com/jgloss/codingconventions.html

The normative document is the one on the Oracle site.
eachKidGetsTen needs to be initialised.

Two things wrong with that advice. First, as written the program does not
require that the variable be initialized. Second, it is initialized in the
program the OP posted:

In the strict sense, it is not initialized but assigned here, but I take the
liberty of assuming you didn't mean it in the strict sense.
 

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

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top