0

When the for loop iterates through the 'parts' array, it just goes straight to the last 'else' statement whether it's an empty index, a period, two periods, or a character. My goal with this for loop is to simply skip over an indice if it contains nothing, a period, or two periods. Why doesn't the very first if statement run?

String[] parts = ["", "a",".","b","..","c",""]
        
Deque<String> a = new LinkedList<>();

for(int i=0; i<parts.length; i++) {
    if (parts[i] == "" || parts[i] == "." || (parts[i] == ".." && a.size() == 0)) 
    {
        i++;
    }
    
    else if (parts[i] == ".." && a.size() >= 1) {
        a.removeFirst();
    }
    else {
        a.addFirst(parts[i]);
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Yena
  • 47
  • 5
  • 3
    Your problem is the `i++`. That will cause you to bump `i` twice. First loop, the string is "", `i` becomes 2. Next time, the string is ".", and `i` becomes 4. Next time, the string is ".." and `a` is empty, so we bump it to 6. Last time, the string is "", we bump `i` and exit the loop. – Tim Roberts Aug 17 '21 at 00:11
  • 4
    [Use `equals()` not `==` to compare strings.](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – John Kugelman Aug 17 '21 at 00:15

2 Answers2

5

So there are a couple things to improve on.

Tim and John suggested a couple improvements like the double iteration of the i variable and using .equals() to compare strings.

I'll also add that this is a great place to use an enhanced for loop which looks like something this

for(String item : array) 
    System.out.println(item);

Where array is a String[] array, and code this prints each item in array on a new line.

And if you just want to skip over "", "." and ".." you can just use one if statement to only add the items to the LinkedList if they are not one of these three.

So you could use something like this:

String[] parts = new String[] {"", "a",".","b","..","c",""};

Deque<String> a = new LinkedList<>();

for(String item : parts)
    if( !(item.equals("") || item.equals(".") || item.equals("..")) )
        a.addFirst(item);
            
System.out.println(a);

The output from this is

[c, b, a]

So for each item in the parts array you check if its not equal to one of "", "." or "..", if its not then add it to the a LinkedList.

I'm not sure what you were trying to do with checking the size of the LinkedList or removing the first item, but I hope this is what you were looking for.

Royginald
  • 125
  • 1
  • 10
2

I'm not 100% sure this will work, but this might help (also when debugging use System.out.println() to deduce the actual problem)

String[] parts = {"", "a",".","b","..","c"," "};
    
    Deque<String> a = new LinkedList<>();

    for(int i=0; i<parts.length;) {
        if (parts[i] == "" && parts[i] == "." & (parts[i] == ".." && a.size() != 0)) 
        {
            i++;
        }
        
        else if (parts[i] == ".." && a.size() >= 1) {
            a.removeFirst();
        }
        else {
            a.addFirst(parts[i]);
        }
    }
}

 

An alternative would be to use a while loop like this

int i = 0;

    String[] parts = {"", "a",".","b","..","c"," "};
    
    Deque<String> a = new LinkedList<>();

    while(i<parts.length) {
        if (parts[i] == "" && parts[i] == "." & (parts[i] == ".." && a.size() == 0)) 
        {
            i++;
        }
        
        else if (parts[i] == ".." && a.size() >= 1) {
            a.removeFirst();
        }
        else {
            a.addFirst(parts[i]);
        }
    }
}

I guess this would work too

tring[] parts = {"", "a",".","b","..","c"," "};
    
    Deque<String> a = new LinkedList<>();

    for(int i=0; i<parts.length; i++) {
        if (parts[i] != "" && parts[i] != "." & (parts[i] != ".." && a.size() != 0)) 
        {
            i--;
        }
        
        else if (parts[i] == ".." && a.size() >= 1) {
            a.removeFirst();
        }
        else {
            a.addFirst(parts[i]);
        }
    }

I'm sorry if I can't help you that much, but I just need more of your code.

Dequog
  • 124
  • 13