I have a searchSales () method that should find all sales metrics that are equal to a given sales metric. The application asks the user to enter this sales figure using the keyboard and search for it. If a sales digit is entered from the keyboard, the application displays the sales / digits indicator, otherwise it will display a corresponding message. Well, I have a code that displays only the first index of an equal sales indicator, for example: an array has elements 1,2,3,3,4,5, and I want to find all the indices [array] = 3. How can I do this ?
public static void searchSales(int search[]){ Scanner input = new Scanner(System.in); System.out.print("Enter sales figure you want to find: "); int target = input.nextInt(); int index = -1; for (int i=0; i<search.length; i++){ if (search[i] == target){ index=i; break; } } if (index == -1){ System.out.println("Sales figure not found"); } else { System.out.printf("Sales figure found at branch %d",index+1); } }
source share