6

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

5 Answers 5

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

The continue you have there is superfluous.
0

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.

Comments

0

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

Comments

0

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

5 Comments

You're right. And sorting will destroy the index needed. Makes it a little messy. Updated.
No need to keep hitting sales.get(start), just compare against target :-) PS you can use listIterator(start)
But how can I make it without using ArrayList<Integer>?
@Nataly - check the other answers if you don't want to use a collection.
@oldrinb - thanks for the suggestion - updated. Not sure listiterator(start) adds anything here because i'll have to iterate through the entire list rather than stop at end.
0

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

4 Comments

Yes, this is better and works correctly!Thank you so much! But what do I need to do in case of highestSales() method?
What do you mean by "what do I need to do in case of highestSales() method"? If you mean what you need to import: import java.util.ArrayList and import java.util.List.
List<Integer> list = new ArrayList<Integer>();- it says that cannot find symbol and the second error in the last line - for (int i : list) System.out.println(i);- incompatible types. How can I fix it?Sorry, but I'm just a beginner in java))
I fixed it.Thanks a lot))I'm trying to finish my application

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.