• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java: "Int is not boolean" error

VuLakAerr

Member
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.
 
"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");
}
 
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.
 
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.
 
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;
 
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.
 
Back
Top