JAVA Question I need help with this code After I add all 5 f
JAVA Question: I need help with this code. After I add all 5 flowers, I remove one so one index goes to null, but won\'t let me refill that index with another flower.
I\'m gettting \"Pack is Full\" even though 3rd index is null. I need help!
import java.util.Scanner;
public class Flowers {
    public static void main(String[] args){
        new Flowers ();
    }
   
    // This will act as our program switchboard
    public Flowers (){
        Scanner input = new Scanner(System.in);
        String[] flowerPack = new String[5];
       
        System.out.println(\"Welcome to my flower pack interface.\");
        System.out.println(\"Please select a number from the options below\");
        System.out.println(\"\");
       
        while(true){
            // Give the user a list of their options
            System.out.println(\"1: Add an item to the pack.\");
            System.out.println(\"2: Remove an item from the pack.\");
            System.out.println(\"3: Sort the contents of the pack.\");
            System.out.println(\"4: Search for a flower.\");
            System.out.println(\"5: Display the flowers in the pack.\");
            System.out.println(\"0: Exit the flower pack interfact.\");
           
            // Get the user input
            int userChoice = input.nextInt();
               
            switch(userChoice){
                case 1:
                    addFlower(flowerPack);
                    break;
                case 2:
                    removeFlower(flowerPack);
                    break;
                case 3:
                    sortFlowers(flowerPack);
                    break;
                case 4:
                    searchFlowers(flowerPack);
                    break;
                case 5:
                    displayFlowers(flowerPack);
                    break;
                case 0:
                    System.out.println(\"Thank you for using the flower pack interface. See you again soon!\");
                    System.exit(0);
            }
        }
       
    }
   private void addFlower(String flowerPack[]) {
        // TODO: Add a flower that is specified by the user
        Scanner input = new Scanner(System.in);
        String flower;
        int position = 0;
        System.out.println(\"Enter flower to add: \");
        flower = input.next();
       
        for(int i = 0; i < flowerPack.length; i++){
            if(flowerPack[i] != null) {
                position++;
                if(position == flowerPack.length) {
                    System.out.println(\"Pack is full\");
                }
            } else {
                flowerPack[i] = flower;
                break;
            }
       }
        //input.close();
       
}
  
    private void removeFlower(String flowerPack[]) {
        // TODO: Remove a flower that is specified by the user
        Scanner input = new Scanner(System.in);
        System.out.println(\"What flower remove? \");
        String flower = input.next();
        for(int i = 0; i < flowerPack.length; i++){
            if(flowerPack[i] != null && flowerPack[i].equals(flower))
                flowerPack[i] = \"null\";
        }
       
    }
Solution
import java.util.Scanner;
public class Flowers {
    public static void main(String[] args){
        new Flowers ();
    }
   
    // This will act as our program switchboard
    public Flowers (){
        Scanner input = new Scanner(System.in);
        String[] flowerPack = new String[5];
       
        System.out.println(\"Welcome to my flower pack interface.\");
        System.out.println(\"Please select a number from the options below\");
        System.out.println(\"\");
       
        while(true){
            // Give the user a list of their options
            System.out.println(\"1: Add an item to the pack.\");
            System.out.println(\"2: Remove an item from the pack.\");
            System.out.println(\"3: Sort the contents of the pack.\");
            System.out.println(\"4: Search for a flower.\");
            System.out.println(\"5: Display the flowers in the pack.\");
            System.out.println(\"0: Exit the flower pack interfact.\");
           
            // Get the user input
            int userChoice = input.nextInt();
               
            switch(userChoice){
                case 1:
                    addFlower(flowerPack);
                    break;
                case 2:
                    removeFlower(flowerPack);
                    break;
                case 3:
                    sortFlowers(flowerPack);
                    break;
                case 4:
                    searchFlowers(flowerPack);
                    break;
                case 5:
                    displayFlowers(flowerPack);
                    break;
                case 0:
                    System.out.println(\"Thank you for using the flower pack interface. See you again soon!\");
                    System.exit(0);
            }
        }
       
    }
// you need to edit your addFlower() method
private void addFlower(String flowerPack[]) {
        // TODO: Add a flower that is specified by the user
        Scanner input = new Scanner(System.in);
        String flower;
// you dont need to write position variable
 //int position = 0;
        System.out.println(\"Enter flower to add: \");
// next() method only read till the space
 //flower = input.next();
// nextLine() read until the end of the line \\0, so always use nextLine()
        flower = input.nextLine();
        for(int i = 0; i < flowerPack.length; i++){
// here we check the string flowerPack,if it\'s null then add flower to flowerPack string
            if(flowerPack[i] == null) {
                flowerPack[i]=flower;
// here you need to break statement when if condition is completed then go to the for loop to increment i and check the condition
                break;
} //if completed
// here checks if integer i is equal to the flowerPack length-1 and flowerPack is not equal to null then pack is full
else if(i==flowerPack[i].length-1 && flowerPack[i] != null) {
System.out.println(\"Pack is full\");
break;
} // if completed
} // for loop completed
 } // method closing bracket
// here you also need to edit removeFlower() method
  private void removeFlower(String flowerPack[]) {
        // TODO: Remove a flower that is specified by the user
        Scanner input = new Scanner(System.in);
        System.out.println(\"What flower remove? \");
        String flower = input.nextLine();
        for(int i = 0; i < flowerPack.length; i++){
            if(flowerPack[i] != null && flowerPack[i].equals(flower))
                flowerPack[i] = \"null\";
//you need to break statement
break;
        }
       
// you need to write else also
else {
System.out.println(\"flower not found in the pack\");
break;
} // closing else
} // closing for loop
} // closing removeFlower() method
// now these methods are add and remove flower correctly




