I need to create a code that checks if the input from the user is equal to a double literal length 3. my if statement is where i am having trouble. Thanks
Scanner stdIn= new Scanner(System.in);
String one;
String two;
String three;
System.out.println("Enter a three character double literal ");
one = stdIn.nextLine();
if (!one.length().equals() "3")
{
System.out.println(one + " is not a valid three character double literal");
}
if (one.length() != 3)
if (!(one.length().equals(3))
Both these ways work.
For more details please refer this.
https://www.leepoint.net/data/expressions/22compareobjects.html
if (!(one.length().equals(3)) {
System.out.println(one + " is not a valid three character double literal");
}
You have to place the 3
as an argument to the equals
function ( it takes an argument).
More common is to use ==
when comparing numbers though.
if (!(one.length() == 3) {
System.out.println(one + " is not a valid three character double literal");
}
or more concise:
if (one.length() != 3) {
System.out.println(one + " is not a valid three character double literal");
}
您不需要使用.equals(),因为length方法返回一个int值。
if ( one.length() != 3 ) { do something; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.