运算符 < 不能应用于 java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16697465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
operator < cannot be applied to java
提问by Irishgirl
As below I've typed javac App.javaon mac Terminal and I see operatorfive errors, all are the same. I'm not sure how to fix it as below and I appreciate your pointers?
如下所示,我在 mac 终端上输入了javac App.java,我看到操作符五个错误,都是一样的。我不知道如何解决它,如下所示,我感谢您的指点?
I've import javabook.*; and this is JAVA code on Textpad.
我已经导入 javabook.*; 这是 Textpad 上的 JAVA 代码。
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 )
终端响应为App.java:28: operator < 不能应用于 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);
}
}
回答by Andrew Martin
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.
原因是您将“theNumber”存储为字符串,然后尝试对其进行整数比较。它不是整数,因此发生错误。
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.
现在,您将 theNumber 存储为一个整数,并且您正在使用扫描仪读取下一个整数并将其存储在 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.
或者,您可以继续使用 String 并简单地将其包装Integer.parseInt()
在您的if/else
语句中,但考虑到您的代码将其存储为int
似乎更具建设性。
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.
编辑 - 注意,必须导入 Scanner 类(如在 OP 提供的代码中,它当前已被注释掉。
回答by matt freake
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.
这就是说 Java 运算符“<”(小于)不能应用于(用于比较)String 和 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.
所以你要问的是“400”<20,这是你在Java中做不到的。您需要theNumber
先将字符串转换为 int。如果你搜索它,你会在这里和互联网上找到很多。
回答by Karthik Prasad
Convert the String to Integer
将字符串转换为整数
int a = Integer.parseInt(theNumber);
Basic rule in Java is that a condition must evaluate to boolean which means if(integer) is wrong
Java 中的基本规则是条件必须评估为布尔值,这意味着 if(integer) 是错误的