0

In my program I'm trying to loop specific actions depending on the value of the String args[0]. I have a conditional tree setup, but no matter what args[0] is, it always chooses the last else option as opposed to the option I want it to be.

Here is the relevant code:

public static void main(String[] args)
{
    int a = Integer.parseInt(args[1]);
    int b = Integer.parseInt(args[2]);
    int c = Integer.parseInt(args[3]);
    int d = Integer.parseInt(args[4]);

    for (int i = -a; i <= a; i++)
    {
        for (int j = -b; j <= b; j++)
        {
            for (int k = -c; k <= c; k++)
            {
                for (int l = -d; l <= d; l++)
                {
                    if (args[0] == "rational-class")
                        rationalClass(a,b,c,d);
                    else if (args[0] == "rational-instance")
                        rationalInstance(a,b,c,d);
                    else if (args[0] == "complex-class")
                        complexClass(a,b,c,d);
                    else if (args[0] == "complex-instance")
                        complexInstance(a,b,c,d);
                    else
                        System.out.println("error");
                }
            }
        }
    }

}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Christian Baker
  • 375
  • 2
  • 7
  • 22

2 Answers2

1

Try comparing the two strings with equals method, i.e.

if (args[0].equals("rational-class")
user2927391
  • 197
  • 2
  • 11
0

If it's a string that you are comparing for you arg[0], you need to do something like this.

    args[0].equals("......")