Java: "Int is not boolean" error

VuLakAerr

Member
Jul 8, 2004
34
0
0
What exactly does this mean?

// The "Chapter6RandomCards" class.
import java.awt.*;
import hsa.Console;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.IOException;

public class RandomCards
{
static Console c; // The output console

public static void main (String[] args) throws IOException
{
c = new Console ();

int card;
// PrintWriter output;

////////
FileOutputStream fis;
////////

try
{

fis = new FileOutputStream ("dice.txt");

//work with the file in here

for (int count = 1 ; count <= 1 ; count++)
{
card = (int) (Math.random () * 5);


// output.println (card);
c.println (card);
}

if (card = 1)
{
c.println ("You rolled an Ace of Spades");
}
if (card = 2)
{
c.println ("You rolled an Ace of Clubs");
}



c.println ("The card you chose is a:");

}
catch (IOException e)
{
// No error message
}
finally
{
// Make sure the resources used are closed
// try { output.close(); } catch ( IOException e ) {}

}
} // main method
} // Chapter6RandomCards class


When I try and executethe program it highlights "if (card = 1)" and "if ( card = 2)"

The error message is: The type of this expression, "int", is not "boolean". What does this mean? The program is simply supposed to generate a single random number, and each different number has a different card assigned to it, with is outputted on the console. How can I fix this error? Thank you.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
You want (card == 1), not (card = 1)

= is an assignment operator
== is a comparison operator
 
Aug 4, 2004
52
0
0
"if (card = 1)" and "if (card = 2)" are highlighted because a single equals sign is used for assignment. You need two, it should look like this:

if (card == 1)
{
c.println ("You rolled an Ace of Spades");
}
if (card == 2)
{
c.println ("You rolled an Ace of Clubs");
}
 

VuLakAerr

Member
Jul 8, 2004
34
0
0
Oh, I see. Ok, thanks, that fixed that error, which spawned a new one.

if (card == 1) - The "card" part of that line is highlighted with the error message: The "variable" card may have been accessed here before having been definitely assigned a value.

This statement assigns the card a value. no?

for (int count = 1 ; count <= 1 ; count++)
{
card = (int) (Math.random () * 5);

what am I doing wrong?

Edit: I had brackets in the wrong place, I think I got it, thanks alot guys.
 

juiio

Golden Member
Feb 28, 2000
1,433
4
81
Also, your for loop does not have the condition that you want, most likely.
 

VuLakAerr

Member
Jul 8, 2004
34
0
0
Thanks for the help guys, finished that assignment. I have another quick question though.

In the program I am doing now, it produces 10 random numbers. It is supposed to output the numbers, as well as add them and find their average. This is what I have so far :

for (int count = 1 ; count <= 10 ; count++)
{
random = (int) (Math.random () * 11);

c.println (random);

very simple, but how do I add the 10 random numbers as well as find the average? I've done this before but completely forget. Thank you.


Edit: Heh, you'd think so, stnicralisk, but believe it or not, I am ahead of the class...We spent the first two months outputting text and doing cheasy animations. All of these math commands and generated data are all new to us.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Something like:

int randomSum = 0;
// You may want to make average a double, depending on what precision is required
int average = 0;

for (int count = 1; count <= 10; count++){
...random = (int) (Math.random() * 11);
...randomSum += random;
...c.println(random);
}

// count is at 11, so we need to decrement it
count--;
average = randomSum / count;
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
You'll need another variable, called it sum or total, and declare it above the loop, set it to 0. Inside the loop, add each "random" to it. Once you're outside of the loop, divide that by the number of iterations (10, in this case). Use a double/long (?) if you want decimal places for the average, integer will arbitrarily round them off.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: VuLakAerr
Hmm, Ok, I sort-of understand. What do you mean by "Inside the loop, add each random to it"

Look at the post above his :p