Reputation: 168
My BlueJ is running the wrong lines in the conditional statement if. My program is:
import java.io.*;
public class version_check
{
public static void main(String args[])throws IOException
{
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your BlueJ version");
String version=read.readLine();
if(version!="3.1.1")
System.out.println("You need to upgrade your BlueJ version");
else
System.out.println("You have the latest BlueJ version");
}
}
Output screen:
Enter your BlueJ version
3.1.1
You need to upgrade your BlueJ version
What mistake am I making?
Upvotes: 0
Views: 242
Reputation: 1
import java.io.*;
public class version_check
{
public static void main(String args[])throws IOException
{
BufferedReader read=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter your BlueJ version");
String version=read.readLine();
if(!version.equals ("3.1.1"))
System.out.println("You need to upgrade your BlueJ
version");
else
System.out.println ("You have the latest BlueJ version");
}
}
Upvotes: 0
Reputation: 27
The only comparison methods i know of for String types are the: 1.>String_variable.equals(String_to_be_checked_with) and the 2.>String_variable.equalsIgnoresCase(String_to_be_checked_with)
the second ignores the case of the string value that has been entered...
the easiest solution to your problem is:
if(version.equals("3.1.1"))
{
System.out.println("You have the latest BlueJ version");
}
else {
System.out.println("You need to upgrade your BlueJ version");
}
Upvotes: 0
Reputation: 323
Use .equals to compare strings.
if(version!="3.1.1")
System.out.println("You need to upgrade your BlueJ version");
to
if(!(version.equals("3.1.1")))
System.out.println("You need to upgrade your BlueJ version");
Upvotes: 1