Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Beginner Problem: Array sorting decreasing element values? Why?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Thanked 0 Times in 0 Posts

    Default Beginner Problem: Array sorting decreasing element values? Why?

    Hello, first post and first problem here. For an assignment, we were asked to put together this one code from the book that would create an array and fill it up with corresponding amount of correct answers (the array with the answers and the answer key is already built in) and from there, we were supposed to sort them by increasing value. I'm sure you'll notice when the sorting part begins. My problem is that the sorting part of my script takes the correct number of answers from the first part, sorts them, and when I call on them to be printed out, they are -2 than what their value should be. For example, from the pre-set array there are 4 'students' with 7 correct answers, but the end of my entire script displays them as 4 5's instead. Please help me point out where the element reduction is taking place.

    public class GradeExam {
    	public static void main(String[] args) {
    		char [][] answers =	{
    			{'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
    			{'D', 'B', 'A', 'B', 'C', 'A', 'E', 'E', 'A', 'D'},
    			{'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'},
    			{'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'},
    			{'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
    			{'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
    			{'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
    			{'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}};
    		char[] keys = {'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'};
    		int[] ArrayF = new int[8];
    		for (int i = 0; i < answers.length; i++) {
    			int correctCount = 0;
    			for (int j = 0; j < answers.length; j++) {
    				if(answers[i][j] == keys[j] )
    			ArrayF[i] = correctCount;
    		for (int a = 0; a < ArrayF.length - 1; a++) {
    			int currentMin = ArrayF[a];
    			int currentMinIndex = a;
    			for (int b = a + 1; b < ArrayF.length; b++) {
    				if (currentMin > ArrayF[b]) {
    					currentMin = ArrayF[b];
    					currentMinIndex = b;
    			if (currentMinIndex != a) {
    				ArrayF[currentMinIndex] = ArrayF[a];
    				ArrayF[a] = currentMin;
    		for (int k = 0; k < ArrayF.length; k++) {
    			System.out.println("Student " + (k+1) + "'s correct count was " + ArrayF[k]);

    Bonus question: How would I go about sorting the student number too?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    On vacation
    Thanked 2,243 Times in 2,215 Posts

    Default Re: Beginner Problem: Array sorting decreasing element values? Why?

    when I call on them to be printed out, they are -2 than what their value should be.
    Can you show the print out or other evidence where this is shown?
    Post the program's output and explain what is wrong with it and show what it should be.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: November 20th, 2011, 01:33 PM
  2. Problem with array values
    By Harry Blargle in forum Collections and Generics
    Replies: 5
    Last Post: September 17th, 2011, 04:05 PM
  3. array element declaring problem
    By jack_nutt in forum Collections and Generics
    Replies: 11
    Last Post: August 1st, 2011, 06:29 PM
  4. problem searching for a String element in a parallel array
    By david185000 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2010, 01:24 PM
  5. Having trouble insert/sorting array values w/ binary searching.
    By bh-chobo in forum Collections and Generics
    Replies: 4
    Last Post: October 8th, 2009, 02:38 AM