-1

I want to set up a main class that pass a string to another class from the prompt dialog on Eclipse.

So I set up a Program argument on "Run Configurations" as a ${string_prompt} variable. When I run my program the input dialog appear and I input a value.

I then retrieve the input on my main class from the index 0 of the String[] args argument:

if ( args.length > 0 ) {b = new BrowserHandler(args[0]);}

and pass it to my BrowserHandler class constructor, that retrieve it and set the String as the input value.

I successively try to call a method named SetBrowser() from the main class. It has to check the validity of the aforementioned String:

    public static void main(String[] args) throws Exception {

        BrowserHandler b;

        if ( args.length > 0 ) {b = new BrowserHandler(args[0]);}
        else b = new BrowserHandler();

        b.setBrowser();
        if ( !b.valid ) System.out.println("Browser not valid");

        else {
            //do something
        }

    }

}

The BrowserHandler class:

public class BrowserHandler {

public String browser;
Boolean valid = true;

public BrowserHandler(String browser){
    this.browser = browser.toLowerCase();
}

public void setBrowser() {
        if ( this.browser == "firefox" ) { 
            System.out.println("...setting firefox");
            this.driver = new FirefoxDriver(); 
        }
        else valid = false;

}

The problem is that the String browser of the browserhandler class get the variable, but when it comes to check the value of the string it always fails the condition, setting valid to false.

I cannot figure it why is that, despite having tried to debug it. Any suggestion that could lead me to the reason of it?

iomv
  • 2,409
  • 18
  • 28

1 Answers1

1

In the if statement of the setBrowser method, the == operator should be replaced by a this.browser.equals("firefox"). The reason is that the == operator compares references and not the actual values of the two variables being compared.

Ryan Fl0w
  • 108
  • 8