javafor-loopif-statementlinked-list

How do I make the else statement to stop repeating output?


I'm just a beginner and badly needed help. I'm trying to make a simple inventory system using linked list with a search feature, I'm stuck on making this feature a complete success because my if else statement is enclosed within a loop and when it doesn't find the element on the list, it prints the output repetitively. It's basically going through every single element and prints the string I assigned on the else statement everytime the program goes through each element. I'm not sure if I explained it well but here's the code that's causing a toll on my mentality:

For reference, fruitMenu is the name of my linked list. The if statement works just fine, it's just the else statement that doesn't.

//case five - search for a fruit within the inventory

 public static void caseFive(){
   System.out.print("What fruit would you like to search? Search for:  ");
         int listSize = fruitMenu.size()-1;
         s.nextLine();
         String userSearch = s.nextLine();
        
         
           for (int j = 0; j <= listSize; j++) 
           {
             if (fruitMenu.get(j).equalsIgnoreCase(userSearch))
             {
               System.out.println(userSearch + " is in the inventory");
               System.out.println(fruitMenu);
               break;
             }
             else {
              System.out.println(userSearch + " is not in the inventory");
             }
           }

The code was originally a while loop but had it changed to for loop cause I was hoping it would work, plus I tried putting a break as well after the else statement which obviously doesn't work. I tried putting

boolean find = false;

and placed if (find = true) within the for loop next to the (fruitMenu.get(j).equalsIgnoreCase(userSearch)) but it was s,till giving me the same issue with the else statement.

Since the if statement works just how I want it to be, I'm just expecting the else statement to print "Cherry is not in the inventory" just once, when the user inputs Cherry and it's not part of the list.

All your help is greatly appreciated.


Solution

  • Since you have the else statement inside your for loop it will print "is not in the inventory" for every fruit that doesn't match the user's search term. You can set a flag to determine if the fruit was found and only print the message outside the for loop if it wasn't found:

    public static void caseFive() {
        System.out.print("What fruit would you like to search? Search for: ");
        int listSize = fruitMenu.size() - 1;
        s.nextLine();
        String userSearch = s.nextLine();
        boolean found = false;
    
        for (int j = 0; j <= listSize; j++) {
            if (fruitMenu.get(j).equalsIgnoreCase(userSearch)) {
                System.out.println(userSearch + " is in the inventory");
                System.out.println(fruitMenu);
                found = true;
                break; // you found the fruit, so you can exit the loop
            }
        }
    
        if (!found) {
            System.out.println(userSearch + " is not in the inventory");
        }
    }
    

    By default your state is that the fruit was not found, you only change this after the fruit was found within your if statement. So if you loop over all fruits without finding the one specified by the user, the code within the if statement is never executed and your state will still be found == false. Therefore, !found will be true and your message will be printed once at the end.