Nataly
Nataly

Reputation: 61

Java - searching all elements indexes in array equal to given number

I have method searchSales() which is supposed to find all sales figures which are equal to the given sales figure.The application asks the user to enter the given sales figure using the keyboard and searches for it. If sales figure entered from the keyboard is found then the application displays sales figure/figures otherwise it displays an appropriate message. Well, i have a code which displays only first index of equal sales figure, for example: array has elements 1,2,3,3,4,5 and I want to find all indexes of [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);
        }
    }

Upvotes: 6

Views: 5215

Answers (5)

arshajii
arshajii

Reputation: 129507

In your case you don't really need to store anything anywhere, something like this should suffice:

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 different story. There, you do have to store stuff:

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);
}

Upvotes: 0

Sridhar
Sridhar

Reputation: 2532

Use Arrays.asList and then use indexOf/lastIndexOf on the List. Something like:

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);
}

Upvotes: 0

rookiepupil
rookiepupil

Reputation: 39

 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");
    }

}

Upvotes: 1

aroth
aroth

Reputation: 54806

Use a List<Integer> to collect your matching indices. Something like:

    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);
    }

Upvotes: 0

Vala
Vala

Reputation: 5674

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

Upvotes: 0

Related Questions