Java - search for all index elements in an array equal to a given number

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); } } 
+6
source share
5 answers
  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; System.out.printf("Sales figure found at branch %d\n",index+1); } } if (index == -1){ System.out.println("Sales figure not found"); } } 
+1
source

Remove the line that says break; (this line causes your code to exit the loop), save each value in an array and print it at the end or print it out instead of storing them in the index variable.

0
source

Use List<Integer> to collect the corresponding indexes. Sort of:

  int target = input.nextInt(); List<Integer> indices = new ArrayList<Integer>(); for (int i=0; i<search.length; i++){ if (search[i] == target){ indices.add(i); } } if (indices.isEmpty()){ System.out.println("Sales figure not found"); } else { System.out.println("Sales figures found: " + indices); } 
0
source

Use Arrays.asList and then use indexOf / lastIndexOf in the list. Sort of:

 ArrayList<Integer> sales = Arrays.asList(search) int start = sales.indexOf(target); int end = sales.lastIndexOf(target); if(start==end){ System.out.printf("Sales figure found at branch %d",start+1); } else{ for(int i=start;i<=end-start;i++) if(sales.get(i)==target) System.out.printf("Sales figure found at branch %d",i+1); } 
0
source

In your case, you do not need to store anything anywhere, something like this should be enough:

 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(); boolean found = false for (int i = 0 ; i < search.length ; i++) if (search[i] == target) { found = true; System.out.printf("Sales figure found at branch %d", i + 1); } if (! found) System.out.println("Sales figure not found"); } 


highestSales is a completely different story. There you need to store things:
 public static void highestSales(int[] nums) { List<Integer> list = new ArrayList<Integer>(); list.add(0); for (int i = 1 ; i < nums.length ; i++) { if (nums[i] > nums[list.get(0)]) { list.clear(); list.add(i); } else if (nums[i] == nums[list.get(0)]) list.add(i); } System.out.println("Highest sales figure " + nums[list.get(0)] + " found at these branches"); for (int i : list) System.out.println(i); } 
0
source

Source: https://habr.com/ru/post/925074/


All Articles