tags:

views:

235

answers:

4

Hi : I am using the following code

String sample = " : : ";
String[] splitTime = sample.split(":");
if ( splitTime[0].trim().equals("") ||
        splitTime[0].trim().equals(null)) {
      System.out.println("Inside Split 0");
      splitTime[0] = "0";
 } else if (splitTime[1].trim().equals("") ||
        splitTime[1].trim().equals(null)) {
      System.out.println("Inside Split 1");
      splitTime[1] = "0";
 }else if (splitTime[2].trim().equals("") ||
        splitTime[2].trim().equals(null)) {
      System.out.println("Inside Split2");
      splitTime[2] = "0";
 }

 System.out.println("Value 1 :"+splitTime[0]);
 System.out.println("Value 2 :"+splitTime[1]);
 System.out.println("Value 3 :"+splitTime[2]);

OUTPUT :
Inside Split 0
Value 1 :0
Value 2 :
Value 3 :
I don know why it is not going into second and third if-else statement. Can anyone help me?

+3  A: 

because if the first if statement is true, and thus is executed, then the rest of the else ifs are not executed. If you want to execute the other else-if statements, why not rewrite them as separate if statements?

String sample = "::";
String[] splitTime = sample.split(":");
if ( splitTime[0].trim().equals("") ||
        splitTime[0].trim().equals(null)) {
      System.out.println("Inside Split 0");
      splitTime[0] = "0";
 }
 if (splitTime[1].trim().equals("") ||
        splitTime[1].trim().equals(null)) {
      System.out.println("Inside Split 1");
      splitTime[1] = "0";
 } 
 if (splitTime[2].trim().equals("") ||
        splitTime[2].trim().equals(null)) {
      System.out.println("Inside Split2");
      splitTime[2] = "0";
 }

 System.out.println("Value 1 :"+splitTime[0]);
 System.out.println("Value 2 :"+splitTime[1]);
 System.out.println("Value 3 :"+splitTime[2]);
Chii
A standard for loop iterating over the array would be help as an alternative.
Spencer K
indeed that is the proper way to do what the example seemed to intend
Chii
+2  A: 

It's because of the "else if".

If you have code that does:

if (a) {
} else if (b) {
}

Then the second branch can never be executed if the first branch was.

Instead you need:

if (a) {
}

if (b) {
}

to make the conditions independent.

BTW, String.trim() can't return null, and you're not guaranteed to get three elements in the output from String.split(). Potentially better code would be:

String sample = " : : ";
String[] splitTime = sample.split(":");
for (int i = 0; i < splitTime.length; ++i) {
    splitTime[i] = splitTime[i].trim();
    if (splitTime[i].length() == 0) {
        splitTime[i] = "0";
    }
}
Alnitak
A: 

This is due to the fundamental operation of an If-Else clause:

if(foo) {
  // A
} else if(bar) {
  // B
}

If foo is true, A will execute, and bar will never be tested. To get the semantics you want, consider just chaining your if blocks:

if(foo){
  // A
}

if(bar) {
  // B
}
Greg D
A: 

try

String sample = " 12 : 59 : 60 ";
// strip spaces and create up to 3 parts
String[] splitTime = sample.trim().split(" *: *", 3);
for (int i = 0; i < splitTime.length; i++)
    System.out.println("Inside " + i + " '" + splitTime[i] + '\'');

Prints

Inside 0 '12'
Inside 1 '59'
Inside 2 '60'
Peter Lawrey