0

I want to run this program once to delete the first "Banana" and run this program the second time to delete the second "Banana". The case is when I now running my code, it will only delete the second "Banana" in the array and stops.

Also I want to print the original array before the delete any elements.I used "System.out.println(" " + array)" but it ends up with showing some garbled code.

public static void main(String args[]){
        String[] array = {"Fish", "Banana", "Carrot", "Pinapple", null, "Banana" };
        String[] newArr = null;
        System.out.println("The string is showing as : " );      
        System.out.println(array.length);
        System.out.println(" " + array);
        for(int i = 0; i < array.length; i++){
            if(array[i] == "Banana"){
                newArr = new String[array.length - 1];
                for(int index = 0; index < i; index++){
                    newArr[index] = array[index];
                }
                for(int j = i; j < array.length - 1; j++){
                    newArr[j] = array[j+1];
                }
            }
        }
        System.out.println("After deleting a element the string is showing as : " );      
        for(int i = 0; i < newArr.length; i++){
            System.out.print(newArr[i] + " ");
        }                
    }
}  
Yifei
  • 11
  • 2
  • One problem is that `array[i] == "Donkey"` should be `array[i].equals( "Banana")`. You should learn about string comparison and fix this. But the problem that's causing your bug is that every time you find a `Banana`, you're making a new array for `newArr`, instead of using the one from the previous iteration. – Dawood ibn Kareem Jul 31 '20 at 02:08
  • They 1) why are you looking for "Donkey" and 2) you are comparing Strings incorrectly. Use `equals()` – WJS Jul 31 '20 at 02:08
  • @WJS That won't help in this case, because all the strings are literals, so `==` will work. The problem is that a new `newArr` is being created for each `Banana/Donkey`. – Dawood ibn Kareem Jul 31 '20 at 02:09
  • @DawoodibnKareem Actually the OP should use `"Banana".equals(array[i])` since the array contains a null. – WJS Jul 31 '20 at 02:09
  • 1
    @WJS Ooh, good point. – Dawood ibn Kareem Jul 31 '20 at 02:10
  • Oh, my bad I changed some of my variables to make it looks more clearly, the question was asking me to remove "donkey" in arrays. I changed my code so it asks to remove "Banana" now. – Yifei Jul 31 '20 at 02:17
  • use `Arrays.toString(array)` to print the array – The Scientific Method Jul 31 '20 at 02:19

3 Answers3

0

Firstly, if you try to create an empty array, do not write String[] newArr = null; change it into

    String[] newArr = new String[array.length];
    //if you want the size of newArr similar to the size of array

Secondly, if you want to print all values in an array, do not print the array object directly

    System.out.println(" " + array); //do not use this
    //if you wish to print all values in array

Instead, print the value one by one

    for(String element: array) {
        System.out.print(" " + element); //this command will print a space
    //followed by one value in array
    }

Thirdly, I assume that if(array[i] == "Donkey") is a typo. Because I do not see String "Donkey" in array

Fourthly, to compare an element of String, we can't use if(array[i] == "Banana") we use if(array[i].equals("Banana")) instead

Lastly, because you put an element of null in array you need to make sure that the element in array is not null before you can compare the value with String "Banana" Therefore we need to write down if(array[i] != null) before if(array[i].equals("Banana"))

The complete codes are

    public static void main(String[] args) {
    String[] array = {"Fish", "Banana", "Carrot", "Pinapple", null, "Banana" };
    String[] newArr = new String[array.length];
    System.out.println("The string is showing as : " );      
    System.out.println(array.length);
    for(String element: array) {
        System.out.print(" " + element);
    }
    for(int i = 0; i < array.length; i++){
        if(array[i] != null){
            if(array[i].equals("Banana")) {
                newArr[i] = null;
            } else {
                newArr[i] = array[i];
            }
        }
    }
    System.out.println("\nAfter deleting a element the string is showing as : " );      
    for(int i = 0; i < newArr.length; i++){
        System.out.print(newArr[i] + " ");
    }
}

I hope this helps. Good luck.

Bram
  • 16
  • 4
0

The simple solution for printing the array will be using System.out.println(Arrays.toString(newArr)). This should take care of printing.

0
public static void main(String args[]){
    String[] array = {"Fish", "Banana", "Carrot", "Pinapple", null, "Banana" };
    String[] newArr = null;
    System.out.println("The string is showing as : " );      
    System.out.println(array.length);
    System.out.println(" " + array);
    for(int i = 0; i < array.length; i++){
        if(array[i].equals("Banana")){
            newArr = new String[array.length - 1];
            for(int index = 0; index < i; index++){
                newArr[index] = array[index];
            }
            for(int j = i + 1; j < array.length - 1; j++){
                newArr[j] = array[j+1];
            }
        }
    }
    System.out.println("After deleting a element the string is showing as : " );      
    for(int i = 0; i < newArr.length; i++){
        System.out.print(newArr[i] + " ");
    }                
}

}

Liu
  • 125
  • 7