Java: Incompatible Types (int/boolean)

33,772

Solution 1

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.

Solution 2

You use an assignment operator:

while(counter2 = counter3)

instead of the equality operator:

while(counter2 == counter3)
Share:
33,772
Adam Hussain
Author by

Adam Hussain

Updated on July 07, 2022

Comments

  • Adam Hussain
    Adam Hussain almost 2 years
    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.

    • Adam Hussain
      Adam Hussain over 10 years
      How would I make the while loop into a proper loop? Right now after applying Mik378's fix, the loop does not activate.
    • Mik378
      Mik378 over 10 years
      What are the current values for counter2 and counter3 if you put a breakpoint at the while line?