Rachit Bhargava
Rachit Bhargava

Reputation: 168

BlueJ String Conditional Error

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

Answers (3)

shreyash mali
shreyash mali

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

Richard Quasla
Richard Quasla

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

Oscar F
Oscar F

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

Related Questions