-2

I'm having trouble getting a variable that is modified during a for loop to retain the new value after exiting the loop.

So I have an array called 'outputP' that stores String values. Then I have a for loop with a nested if statement that checks to see if another String value matches one of the strings in outputP and then sets a value for the int variable 'visitCode.':

 String[] outputP = { "SC", "BL", "V04", "V06" };

 int index = folder.getName().indexOf("_R");
 String[] split = folder.getName().substring(index +1).split("_");
 subjectInfo = new String[]{folder.getName().substring(0,index), split[0], split[1]};

 if (subjectInfo[0].charAt(0) != 'S'
     || subjectInfo[1].charAt(0) != 'R'
     || Arrays.asList(outputP).contains(subjectInfo[2] != true ){ 

     //Code here outputs error message
 }

 int groupCode;
 int visitCode = 0;

 groupCode = Integer.parseInt(subjectInfo[1].substring(1));

 for (int i = 0; i < outputP.length; i++)
 {
      if (subjectInfo[2] == outputP[i])
      {
           visitCode = i + 1;
      }
 }

During debugging I've verified during one run that outputP[0] and subjectInfo[2] are both "SC", when I step through the code, it skips over the if statement and visitCode is left equaling 0. I also included above that first if statement, because, it functions as you would expect it to, recognizing that subjectInfo[2] (which was set to "SC") is a string that is also in the outputP array. Does anyone see something that I'm missing that would cause the compiler to skip over the nested if statement?

Thanks in advance to anyone who can help.

wrigley06
  • 349
  • 2
  • 5
  • 18
  • Use ``.equals()`` instead of ``==`` when comparing strings for equality. The former looks for equality of the string's content; the latter looks for object references to be equal. – Keith Aug 19 '15 at 16:42
  • Along with the error stated above consider this as well: In the following code: if (subjectInfo[0].charAt(0) != 'S' || subjectInfo[1].charAt(0) != 'R' || Arrays.asList(outputP).contains(subjectInfo[2] != true ){ } Change `Arrays.asList(outputP).contains(subjectInfo[2] != true )` to `Arrays.asList(outputP).contains(subjectInfo[2]) != true ` – Rezwan Azfar Haleem Aug 19 '15 at 16:44

1 Answers1

2

You should not use == to compare strings. You should use String.equals() or String.equalsIgnoreCase()

gnomed
  • 5,483
  • 2
  • 26
  • 28