0

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?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Rachit Bhargava
  • 170
  • 1
  • 12

3 Answers3

1

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");  
Oscar F
  • 323
  • 2
  • 9
  • 21
0

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");
}
0
     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");
            }
           }
tgdavies
  • 10,307
  • 4
  • 35
  • 40
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Prince Vegeta Nov 07 '19 at 11:23