boolean statement

Discussion in 'Java' started by K, Oct 15, 2012.

  1. K

    K Guest

    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");

    }

    }
    K, Oct 15, 2012
    #1
    1. Advertising

  2. K <> wrote:
    > 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");


    > }
    glen herrmannsfeldt, Oct 15, 2012
    #2
    1. Advertising

  3. K

    Eric Sosman Guest

    On 10/14/2012 8:44 PM, K wrote:
    > 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.

    >
    > }
    >
    > }
    >



    --
    Eric Sosman
    d
    Eric Sosman, Oct 15, 2012
    #3
  4. K

    K Guest

    On Sunday, October 14, 2012 5:57:12 PM UTC-7, glen herrmannsfeldt wrote:
    > K <> wrote:
    >
    > > 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");

    >
    >
    >
    > > }


    what do you mean? How do you use the value of the variable? how would I fix my code?
    K, Oct 15, 2012
    #4
  5. On Sun, 14 Oct 2012 21:04:55 -0400, Eric Sosman
    <> wrote:

    >On 10/14/2012 8:44 PM, K wrote:
    >> 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.


    (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
    Gene Wirchenko, Oct 15, 2012
    #5
  6. K

    K Guest

    Thanks now my program works
    K, Oct 15, 2012
    #6
  7. On Sun, 14 Oct 2012 19:55:23 -0700 (PDT), K <> wrote:

    >Thanks now my program works


    You are welcome.

    Sincerely,

    Gene Wirchenko
    Gene Wirchenko, Oct 15, 2012
    #7
  8. K

    Lew Guest

    Lew, Oct 15, 2012
    #8
  9. K

    Roedy Green Guest

    On Sun, 14 Oct 2012 17:44:31 -0700 (PDT), 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

    eachKidGetsTen needs to be initialised.
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The iPhone 5 is a low end Rolex.
    Roedy Green, Oct 15, 2012
    #9
  10. K <> wrote:
    > On Sunday, October 14, 2012 5:57:12 PM UTC-7, glen herrmannsfeldt wrote:
    >> K <> wrote:
    >> > 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.

    > 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.
    Andreas Leitgeb, Oct 15, 2012
    #10
  11. K

    Jeff Higgins Guest

    On 10/14/2012 10:55 PM, K wrote:
    > Thanks now my program works, you may proceed.
    Jeff Higgins, Oct 15, 2012
    #11
  12. K

    Lew Guest

    Roedy Green wrote:
    > 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:

    >> eachKidGetsTen = gumballsPerKid >= 10;


    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.

    --
    Lew
    Lew, Oct 15, 2012
    #12
  13. K

    Lew Guest

    Jeff Higgins wrote:
    >K wrote:
    >> Thanks now my program works, you may proceed.


    Yeah, right?

    --
    Lew
    Lew, Oct 15, 2012
    #13
  14. On Monday, October 15, 2012 2:44:31 AM UTC+2, K wrote:
    > Why does my boolean statement have an error message next to it in eclipse?


    Just a general advice: it usually helps _a lot_ if you include the error you are seeing in your posting. You can even copy and paste it from Eclipse.

    http://www.catb.org/~esr/faqs/smart-questions.html

    Cheers

    robert
    Robert Klemme, Oct 16, 2012
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    802
  2. Jay McGavren
    Replies:
    11
    Views:
    1,130
    Alan Krueger
    Jan 16, 2006
  3. J Leonard
    Replies:
    4
    Views:
    12,668
    Mark Space
    Jan 19, 2008
  4. dpapathanasiou
    Replies:
    1
    Views:
    245
    Terry Reedy
    Nov 12, 2008
  5. Metre Meter
    Replies:
    7
    Views:
    366
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page