Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator < cannot be applied to java [closed]

Tags:

java

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);
    }
}
like image 471
Irishgirl Avatar asked Aug 24 '26 10:08

Irishgirl


2 Answers

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.

like image 196
Andrew Martin Avatar answered Aug 27 '26 00:08

Andrew Martin


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.

like image 40
matt freake Avatar answered Aug 27 '26 00:08

matt freake