user820913
user820913

Reputation: 619

I get "Type mismatch cannot convert from int to boolean" despite not using boolean

I am doing an excercise in the book "Java how to program". I am supposed to make an application that simulates coin-tossing. I am supposed to make a method (flip) which returns randomly a side for the coin. I have desided to make the method return either 1 or 2, and in the main-method I "convert" the values to mean one of the sides of the coin. The problem is that I get an error message that says: "Type mismatch -cannot convert from int to boolean". I really think that I am operating only with integers all the way, and canot see how the booleans come in.

The code is the following:

import java.util.Random;

public class Oppgave629 
{

    public static void main(String[] args) 
    {
        int command = 1;
        int heads = 0;
        int tails = 0;
        while (command != -1)
        {
            System.out.print("Press 1 to toss coin, -1 to exit:");
            int coinValue = flip();
            if (coinValue = 1) {System.out.println("HEADS!"); heads++;}
            if (coinValue = 2) {System.out.println("TAILS!"); tails++;}
            System.out.printf("Heads: %d", heads); System.out.printf("Tails: %d", tails);
        }
    }

    static int flip()
    {
        int coinValue;
        Random randomValue = new Random();
        coinValue = 1 + randomValue.nextInt(2);
        return coinValue;
    }
}

Upvotes: 5

Views: 117659

Answers (5)

Daniel Isaev
Daniel Isaev

Reputation: 11

if (coinValue == 1) {System.out.println("HEADS!"); heads++;} 
if (coinValue == 2) {System.out.println("TAILS!"); tails++;}

instead of

 if (coinValue = 1) {System.out.println("HEADS!"); heads++;}    
 if (coinValue = 2) {System.out.println("TAILS!"); tails++;}    

with the 2 equal signs

Upvotes: 0

whiterook72
whiterook72

Reputation: 11

I ran into this error b/c i had i=4 in a for statement i.e. "for (i=1;i=4;i++)...."

you can't use an equal to "=" in the for command for the stop value.

Upvotes: 0

GETah
GETah

Reputation: 21459

This one is part of the most common and dangerous programming errors developers do. In the old days, compilers (such as C compiler) won't complain about if(coinValue = 1) statement as it will affect coinValue to 1 and always evaluate the condition to true as it equals 1 Fortunately, the Java compiler catches this error and does not let you do this. As stated in the answers above, change if (coinValue = 1) to if (coinValue == 1) and your problem should be fixed.

Upvotes: 1

Matt Lacey
Matt Lacey

Reputation: 8255

You're using an assignment (=) operator instead of a comparison operator (equality ==) in your if statements:

if (coinValue = 1)

Should be

if (coinValue == 1)

An if statement expects a boolean expression, you're assigning 1 to coinValue and it's then trying to interpret this as a boolean to process the conditional.

Upvotes: 6

b3h47pte
b3h47pte

Reputation: 186

Your code

if (coinValue = 1) {System.out.println("HEADS!"); heads++;}
if (coinValue = 2) {System.out.println("TAILS!"); tails++;}

Should be

if (coinValue == 1) {System.out.println("HEADS!"); heads++;}
if (coinValue == 2) {System.out.println("TAILS!"); tails++;}

You're assigning an int type to coinValue and that is being evaluated as a bool inside the if statement.

Upvotes: 16

Related Questions