Re: another java question

  • From: "Fred Rains" <fred58@xxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Sun, 2 Dec 2007 23:59:57 -0500

Hi Alex, here is an example and an explanation from the book Learning Java, 3rd Edition that might answer your question:


   void myMethod {
     int foo;
     if ( someCondition ) {
       foo = 42;
       ...
     }
     foo += 1;   // Compile-time error, foo may not be initialized
   }

In this example, foo is initialized only if someCondition is true . The compiler doesn't let you make this wager, so it flags the use of foo as an error.


Fred

----- Original Message ----- From: "Alex Parks" <mehgcap@xxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Sunday, December 02, 2007 11:23 PM
Subject: RE: another java question


Out of curiosity, why can you say
int b;
or
char c;
but you must give a boolean a value?
Finally done with blackjack!!!! I will have no more questions with which to bug you all... until the next project.

Have a great day,
Alex

----- Original Message -----
From: "Sina Bahram" <sbahram@xxxxxxxxx
To: <programmingblind@xxxxxxxxxxxxx
Date sent: Sun, 2 Dec 2007 23:16:07 -0500
Subject: RE: another java question

Initialize  the boolean to something, for example: false.

Take care,
Sina

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex
Parks
Sent: Sunday, December 02, 2007 9:33 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: another java question

Hi all,
Sorry, but I have just one more question about this blackjack
program.  For
some extra credit I can include a function that adds 11 to a hand
with an
ace when the total of the hand is below
11 and adds 1 with an ace when the total is more than 11.  I have
this
function inside a method called getHandTotal().  I use a boolean
called ace
and set it to true if the adding loop encounters an ace so that,
after the
loop, I can use an if statement whose condition is that the
handTotal was
greater than
11 and ace==true.  Inside this I subtract 10 from the hand (same
as
subtracting the 11 that was added in the loop and then adding the
one I
want).  The problem is that the compiler says that ace might not
have been
initialized, but just after starting the method I say boolean
ace; Is it
because I am setting ace to true inside a loop or something? I
have no idea
why it won't work.  All times I call ace it is lower case.  I
have tried
just saying if (total>11 && ace and if (total>11 && ace==true but
both
result in that same error.  Thanks for any help.

Have a great day,
Alex
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: