0

The problem is that I can't properly display an output.
Input:

6
Berik 78
Zeynep 100
Saniya 75
Daulet 45
Abay 96
Andrey 75

Expected Output:

1) Zeynep: 100
2) Abay: 96
3) Berik: 78
4) Saniya: 75
5) Andrey: 75
6) Daulet: 45

My Output:

1) Zeynep: 100
2) Abay: 96
3) Berik: 78
4) Saniya: 75
4) Andrey: 75
5) Daulet: 45

As you can see, the numbering is incorrect. Bug must be somewhere in incrementation logic when two points are equal. I pass numbering as an argument to the printMax method

import java.util.ArrayList;
import java.util.Scanner;

public class a1_t13 {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int num = input.nextInt();
    ArrayList<Integer> scores_unsorted = new ArrayList<>();
    ArrayList<String> names_unsorted = new ArrayList<>();


    for (int i=0; i<num; i++) {
        String value = input.next();
        int numValue = input.nextInt();
        scores_unsorted.add(numValue);
        names_unsorted.add(value);
    }

    int b = 1;
    for (int z = 0; z<num;z++) {
        int c = getMax(scores_unsorted);
        printMax(c, names_unsorted, scores_unsorted, b);
        b++;     //<================ HERE I'M DOING AN INCREMENTATION

    }

}

public static int getMax(ArrayList list) {
    int max = 0;
    for(int i=0;i<list.size();i++) {
        if (max< (int)list.get(i)) {
            max = (int) list.get(i);
        }
    }
    return max;
}


public static void printMax(int score, ArrayList names_unsorted, ArrayList scores_unsorted, int order) {
    for (int i=0;i<names_unsorted.size();i++) {
        if ((int) scores_unsorted.get(i) == score) {
            int score_index = scores_unsorted.indexOf(Integer.valueOf(score));
            System.out.println(order+")"+names_unsorted.get(score_index).toString()+": "+score);
            scores_unsorted.remove(score_index);
            names_unsorted.remove(score_index);
        }
    }
}

}

2
  • 2
    That looks all correct on the surface. I would start by adding debug print statements, like printing that b value within your loop. Commented Feb 9, 2019 at 8:11
  • there is already a loop index - z why not use it? just pass z+1 to the method Commented Feb 9, 2019 at 8:18

3 Answers 3

2

After removing 100, 96 and 78, both lists have 3 elements. Now 75 is the max.

printMax(c, names_unsorted, scores_unsorted, b) method is called with c=75 and b = 4.

Now in printMax() this condition will be true for two elements:

if ((int) scores_unsorted.get(i) == score)

as score is 75 now and list has also two 75. So you should break the loop if already found one.

Your if condition block should be like this:

if ((int) scores_unsorted.get(i) == score) {
    int score_index = scores_unsorted.indexOf(Integer.valueOf(score));       
    System.out.println(order+")"+names_unsorted.get(score_index).toString()+": "+score);
    scores_unsorted.remove(score_index);
    names_unsorted.remove(score_index);
    break;
}
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is in your printMax method, because you iterate over all names and two have the same count and you print out both with the same order number. If you break your loop after the first found it will work.

 public static void printMax(int score, ArrayList names_unsorted, ArrayList scores_unsorted, int order) {
    for (int i = 0; i < names_unsorted.size(); i++) {
        if ((int) scores_unsorted.get(i) == score) {
            int score_index = scores_unsorted.indexOf(Integer.valueOf(score));
            System.out.println(order + ")" + names_unsorted.get(score_index).toString() + ": " + score);
            scores_unsorted.remove(score_index);
            names_unsorted.remove(score_index);
            break;
        }
    }
}

Comments

0

The problem is that in printMax function you iterate through the whole array even if you find and print the maximum record you're searching for and you're increasing the value of b after going out of loop. The simplest way to make it work as you want is to put break at the end of if body:

public static void printMax(int score, ArrayList names_unsorted, ArrayList scores_unsorted, int order) {
    for (int i=0;i<names_unsorted.size();i++) {
        if ((int) scores_unsorted.get(i) == score) {
            int score_index = scores_unsorted.indexOf(Integer.valueOf(score));
            System.out.println(order+")"+names_unsorted.get(score_index).toString()+": "+score);
            scores_unsorted.remove(score_index);
            names_unsorted.remove(score_index);
            break;
        }
    }
}

Comments

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.