Adam Hussain
Adam Hussain

Reputation: 11

Java: Incompatible Types (int/boolean)

import java.io.*;
public class AdamHmwk4 {
    public static void main(String [] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int counter1;
        int counter2;
        int counter3;
        String answer = "";

        System.out.println("Welcome to Adam's skip-counting program!");
        System.out.println("Please input the number you would like to skip count by.");
        counter1 = Integer.parseInt(br.readLine());

        System.out.println("Please input the number you would like to start at.");
        counter2 = Integer.parseInt(br.readLine());

        System.out.println("Please input the number you would like to stop at.");
        counter3 = Integer.parseInt(br.readLine());

        System.out.println("This is skip counting by" + counter1 + ", starting at" + counter2  + "and ending at" + counter3 +":");

        while (counter2 = counter3) {
            counter2 = counter2 + counter1;
            counter3 = counter2 + counter1;
        }
    }
}

I am trying to make skip-counting program. When I compile this code, the line while(counter2 = counter3){ shows up as a Incompatible Types error. The compiler says it found an "int" but it requires a "boolean". Please keep in mind that I am a newbie, so I have not learned booleans in my Java class yet.

Upvotes: 0

Views: 41838

Answers (3)

Juned Ahsan
Juned Ahsan

Reputation: 68715

Here is the problem:

               while(counter2 = counter3)

= is used for assignment and post this statement, counter2 will be assigned the value of counter3. Hence your while loop will not behave the way you want. You need to use == for comparing counter2 to counter 3

               while(counter2 == counter3)

Upvotes: 0

Mik378
Mik378

Reputation: 22171

You use an assignment operator:

while(counter2 = counter3)

instead of the equality operator:

while(counter2 == counter3)

Upvotes: 1

rgettman
rgettman

Reputation: 178253

You can't compare values with =, which is the assignment operator. Use == to compare your values. Change

while(counter2 = counter3){

to

while(counter2 == counter3){

Here's an introductory page for Java operators.

Upvotes: 3

Related Questions