As below I've typed javac App.java on mac Terminal and I see operator five errors, all are the same. I'm not sure how to fix it as below and I appreciate your pointers?
I've import javabook.*; and this is JAVA code on Textpad.
import javabook.*;
//import java.util.Scanner;
class App
{
public static void main(String args[])
{
//declare variable
String theNumber;
//declare object
Scanner someInput;
//input
System.out.println("Please enter area size : ");
someInput = new Scanner(System.in);
theNumber = someInput.nextLine();
//processing
if ( theNumber < 20 )
{
System.out.println( "It is too small." ) ;
}
else if ( theNumber > 20 && theNumber < 40 )
{
System.out.println( "It is perfect size." ) ;
}
else if ( theNumber > 40 && theNumber < 60 )
{
System.out.println( "It is too big." ) ;
}
//close the program without error
System.exit(0);
}
}
Terminal response as App.java:28: operator < cannot be applied to java.lang.String,int if ( theNumber < 20 )
I would appreciate your help?
UPDATED:
import javabook.*; //Same result Scanner or javabook. Tried both and it worked.
import java.util.Scanner; //this is required
class App
{
public static void main(String args[])
{
//declare variable
//String theNumber;
//int theNumber = Integer.parseInt(someInput.nextLine());
int theNumber; //need to convert your string theNumber to an int first. If you search for that, you'll find lots, both here and on the internet generally.
int a = Integer.parseInt(theNumber);
//theNumber = someInput.nextInt(); //this is commented out so now down to two errors
//declare object
Scanner someInput;
//input
System.out.println("Please enter area size : ");
someInput = new Scanner(System.in);
theNumber = someInput.nextLine();
//processing
if ( theNumber < 20 )
{
System.out.println( "It is too small." ) ;
}
else if ( theNumber > 20 && theNumber < 40 )
{
System.out.println( "It is perfect size." ) ;
}
else if ( theNumber > 40 && theNumber < 60 )
{
System.out.println( "It is too big." ) ;
}
//close the program without error
System.exit(0);
}
}
The reason why is you are storing "theNumber" as a String and then trying to use integral comparisons on it. It is not an integer, and thus an error is occuring.
Instead, the following would work:
int theNumber;
theNumber = someInput.nextInt();
Now, you are storing theNumber as an Integer and you are using the scanner to read the next integer in and store it in theNumber.
Alternatively, you could continue to use the String and simply wrap it with Integer.parseInt() in your if/else statements, but given your code storing it as an int seems far more constructive.
Note that now the code is checking that the user input is an integer. If it is not, an error will be thrown.
Edit - Note, the Scanner class must be imported (as in the code provided by OP it is currently commented out.
The error message for this is pretty explanatory.
operator < cannot be applied to java.lang.String,int
This is saying that the Java operator '<' (less than), can't be applied to (used to compare) String and an int.
So you are trying to ask, is "400" < 20, which you can't do in Java. You'd need to convert your string theNumber to an int first. If you search for that, you'll find lots, both here and on the internet generally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With