-1

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?

2
  • Try to solve this using a foreach-loop: foreach (String item in splitTime) { /* do the work for each item */ }
    – vgru
    Apr 24, 2009 at 12:02
  • 1
    foreach is no use here because the strings are immutable and the original values need to be overwritten, using the array index.
    – Alnitak
    Apr 24, 2009 at 12:09

5 Answers 5

3

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";
    }
}
2

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]);
2
  • A standard for loop iterating over the array would be help as an alternative. Apr 25, 2009 at 2:00
  • indeed that is the proper way to do what the example seemed to intend
    – Chii
    Apr 25, 2009 at 6:32
1

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
}
0

In nested if else-if statements, if one statement is executed then others are not executed. In your case, the first if expression is always true so it executes and thus other two never be executed.

-1

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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.