0

I keep getting errors when I try to compile these codes below, I'm currently using JCreator.

import java.io.*;

public class Number //class name here, same as file name

 {
 public Number()throws IOException{//constructor, place class name here
 // use BufferedReader class to input from the keyboard
 // declare a variable of type BufferedReader
 BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
 //declare variable for input
 String inputString;

int number;
int counter;
int square;
int cube;
String goodMessage = "Thank you";
String badMessage = "Sorry";

//begin houseKeeping()
System.out.print("Please input number: ");
inputString = input.readLine();
number = Integer.parseInt(inputString);

//begin squareCube()
counter = 0;
while ((counter = 0)&&(number > 0)) {
    square = number*number;
    cube = number*number*number;
    System.out.print(square);
    System.out.print(cube);
}
if (counter = counter + 1);
if (counter < 3);
System.out.print("Enter input number: ");

//begin finishUp()
if (number > 0)
    System.out.println(goodMessage);

    else 
    System.out.println(badMessage);

 }//end constructor

 public static void main(String [] args) throws IOException // main method

 {
 new Number(); //class constructor name
 } // end the main method
 } // end the program

Error:

--------------------Configuration: <Default>--------------------
D:\INFO\INFO 1391\Number.java:27: error: bad operand types for binary operator '&&'
    while ((counter = 0)&&(number > 0)) {
                        ^
  first type:  int
  second type: boolean
1 error

Process completed.

4 Answers 4

2
counter = 0

should be

counter == 0
Sign up to request clarification or add additional context in comments.

Comments

2

You can't use the = operator to compare values; that's the assignment operator. Use == to compare your int values:

while ((counter == 0)&&(number > 0)) {

The assignment operator here evaluates to an int, yielding the error message that you received.

3 Comments

Thanks, I replaced with == and I was able to run the codes, however when I entered numbers, it kept looping and never stops :(
Inside your while loop, you never change number or counter so there is no way for it to exit out of the while loop.
Thank you for the respond, Is there anyway I can get it to print out "goodMessage"?
0

Where is the question?

There are so many errors in this code... To check if two integers are equal you have to write == not =. (In while and in if).

1 Comment

Note also, even once the conditions are fixed so that they compile, half are useless. if (counter < 3); does nothing unless counter < 3, at which point it does...still nothing. The semicolon at the end constitutes an empty statement; it's like saying if (counter < 3) { }.
0

while ((counter = 0)&&(number > 0)) is never true, because (counter = 0) assigns counter to be 0 and the value of that statement is the value of counter: 0. And 0 is int and can't be converted to boolean.

2 Comments

(counter = 0 &&...) isn't false. It wouldn't even compile, cause Java doesn't implicitly convert to boolean.
you are right... i was in c-land in my thoughts... i change my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.