Question

im having some trouble with another program (im just not good with modules and arrays really). can anyone help me to get it working correctly? been working on it all night and im extremely tired... anyway here is the code ive done so far. the problem asks:

Design a program that asks the user to enter 10 golf scores. The scores should be stored in an Integer array. Sort the array in ascending order and display its contents.

import java.util.Scanner;

public class SortedGolfScoresIB
{
    public static void main(String[] args)
    {
        //local variables
        final int SIZE = 10;
        int[] scores = new int[SIZE];

        // get the scores
        getScores(scores, SIZE);

        // sore the scores in ascending order
        insertionSort(scores, SIZE);

        //display the results in ascending order
        displayScores(scores, SIZE);
    }

    // the getScoresmodule prompts the user for
    // golf scores to populare the scores array.

    public static void getScores(int scores[], int size)
    {
        // local variable for loop index
        int index;


        // get the scores
        for (index = 0; index <=size; size--)
        {
            System.out.println("Enter golf score number " + index +1 + ":");
            Scanner keyboard = new Scanner(System.in);
            scores[index] = keyboard.nextInt();
        }

        }
        // the insertionSort module sorts the contents of the  array
        // in ascending order
        public static void insertionSort(int  array[], int size)
        {
            //local variables
            int index;
            int scan;
            int unsortedValue;

            for (index = 1; index <=size; size--)
            {
                unsortedValue = array[index];
                scan = index;
            }

            while (scan > 0 && array[scan-1] < array [scan])
            {
                swap (array[scan-1], array[scan]);
                scan = scan -1;
            }

        array[scan] = unsortedValue;
        }

        //the swap module swaps the contents of its two arguments
        public static void swap(int a, int b)
        {
            int temp;
            //swap a and b
            temp = a;
            a = b;
            b = temp;
        }

        // the display scores module displays the
        // golve scores in the scores array
        public static void displayScores(int scores[], int size)
        {

            // local variable for loop index
            int index;

            //display the scores
            System.out.println ("here are the scores: ");
            for (index=0; index <= size; size--)
            {
                System.out.println(scores[index]);
            }
        }
    }

can anyone help me to get this working and error free?

Edit1: i updated the code and here is what i have now

import java.util.Scanner;

public class SortedGolfScoresIB
{
    public static void main(String[] args)
    {
        //local variables
        final int SIZE = 10;
        int[] scores = new int[SIZE];

        // get the scores
        getScores(scores, SIZE);

        // sore the scores in ascending order
        insertionSort(scores, SIZE);

        //display the results in ascending order
        displayScores(scores, SIZE);
    }

    // the getScoresmodule prompts the user for
    // golf scores to populare the scores array.

    public static void getScores(int scores[], int size)
    {
        // local variable for loop index
        int index;


        // get the scores
        for (index = 0; index <=size; index++)
        {
            System.out.println("Enter golf score number " + (index +1) + ":");
            Scanner keyboard = new Scanner(System.in);
            scores[index] = keyboard.nextInt();
        }

        }
        // the insertionSort module sorts the contents of the  array
        // in ascending order
        public static void insertionSort(int  array[], int size)
        {
            //local variables
            int index;
            int scan;
            int unsortedValue;

            for (index = 1; index <=size; index++)
            {
                unsortedValue = array[index];
                scan = index;
                array[scan] = unsortedValue;

                }

            while (scan > 0 && array[scan-1] < array [scan])
            {
                swap (array[scan-1], array[scan]);
                scan = scan -1;
            }

        }

        //the swap module swaps the contents of its two arguments
        public static void swap(int a, int b)
        {
            int temp;
            //swap a and b
            temp = a;
            a = b;
            b = temp;
        }

        // the display scores module displays the
        // golve scores in the scores array
        public static void displayScores(int scores[], int size)
        {

            // local variable for loop index
            int index;

            //display the scores
            System.out.println ("here are the scores: ");
            for (index=0; index <= size; index++)
            {
                System.out.print(scores[index]);
            }
        }
    }

the error im getting now is that the "scan" in my while loop is not initialized.

edit 2:

got the program to work and compile correctly, but its not outputting all of the scores at the end in ascending order like it should.

import java.util.Scanner;

public class SortedGolfScoresIB
{
    public static void main(String[] args)
    {
        //local variables
        final int SIZE = 10;
        int[] scores = new int[SIZE];

        // get the scores
        getScores(scores, SIZE);

        // sore the scores in ascending order
        insertionSort(scores, SIZE);

        //display the results in ascending order
        displayScores(scores, SIZE);
    }

    // the getScoresmodule prompts the user for
    // golf scores to populare the scores array.

    public static void getScores(int scores[], int size)
    {
        // local variable for loop index
        int index;


        // get the scores
        for (index = 0; index <=size; index++)
        {
            System.out.println("Enter golf score number " + (index +1) + ":");
            Scanner keyboard = new Scanner(System.in);
            scores[index] = keyboard.nextInt();
        }

        }
        // the insertionSort module sorts the contents of the  array
        // in ascending order
        public static void insertionSort(int  array[], int size)
        {
            //local variables
            int index;
            int scan;
            int unsortedValue;

            for (index = 1; index <=size; index++)
            {
                unsortedValue = array[index];
                scan = index;

                array[scan] = unsortedValue;



            while (scan > 0 && array[scan-1] < array [scan])

                swap (array[scan-1], array[scan]);
                scan = scan -1;
            }

        }

        //the swap module swaps the contents of its two arguments
        public static void swap(int a, int b)
        {
            int temp;
            //swap a and b
            temp = a;
            a = b;
            b = temp;
        }

        // the display scores module displays the
        // golve scores in the scores array
        public static void displayScores(int scores[], int size)
        {

            // local variable for loop index
            int index;

            //display the scores
            System.out.println ("here are the scores: ");
            for (index=0; index <= size; index++)
            {
                System.out.print(scores[index]);
            }
        }
    }

code compiles and works, but its not outputting the contents of the array. im getting an out of bounds error.

edit 3: fixed the out of bounds error by changing "index <= size" to "index < size". but error im getting now is that my code just stops after all 10 scores were entered and not displaying the contents of the array as i mentioned before.

import java.util.Scanner;

public class SortedGolfScoresIB
{
    public static void main(String[] args)
    {
        //local variables
        final int SIZE = 10;
        int[] scores = new int[SIZE];

        // get the scores
        getScores(scores, SIZE);

        // sore the scores in ascending order
        insertionSort(scores, SIZE);

        //display the results in ascending order
        displayScores(scores, SIZE);
    }

    // the getScoresmodule prompts the user for
    // golf scores to populare the scores array.

    public static void getScores(int scores[], int size)
    {
        // local variable for loop index
        int index;


        // get the scores
        for (index = 0; index <=size; index++)
        {
            System.out.println("Enter golf score number " + (index +1) + ":");
            Scanner keyboard = new Scanner(System.in);
            scores[index] = keyboard.nextInt();
        }

        }
        // the insertionSort module sorts the contents of the  array
        // in ascending order
        public static void insertionSort(int  array[], int size)
        {
            //local variables
            int index;
            int scan;
            int unsortedValue;

            for (index = 1; index <=size; index++)
            {
                unsortedValue = array[index];
                scan = index;

                array[scan] = unsortedValue;



            while (scan > 0 && array[scan-1] < array [scan])

                swap (array[scan-1], array[scan]);
                scan = scan -1;
            }

        }

        //the swap module swaps the contents of its two arguments
        public static void swap(int a, int b)
        {
            int temp;
            //swap a and b
            temp = a;
            a = b;
            b = temp;
        }

        // the display scores module displays the
        // golve scores in the scores array
        public static void displayScores(int scores[], int size)
        {

            // local variable for loop index
            int index;

            //display the scores
            System.out.println ("here are the scores: ");
            for (index=0; index <= size; index++)
            {
                System.out.print(scores[index]);
            }
        }
    }

3rd and hopefully last edited code. can anyone help me with this last bit?

Was it helpful?

Solution

All the for loops in your program are accessing the same index from the scores array.

Change the for loop: size-- to index++ as follows

for (index = 0; index <=size; index++) <br>
{
 //Your code
}

OTHER TIPS

Well, normally I would post this as comment, since I'm still not sure, what you exactly want; however there's this much code, that I can't.

But are you looking for a program doing exactly this:

 import java.util.Scanner;
 import java.util.LinkedList;
 import java.util.Comparator;

 public class SortedGolfScoresIB {

     public static void main(String[] args) {
         //local variables
         final int SIZE = 10;
         LinkedList<Integer> scores;
         // get the scores
         scores = getScores(SIZE);
         // sore the scores in ascending order
         insertionSort(scores);
         //display the results in ascending order
         displayScores(scores);
    }

    // the getScoresmodule prompts the user for
    // golf scores to populare the scores array.
    public static LinkedList<Integer> getScores(int size) {
        LinkedList<Integer> result = new LinkedList<Integer>();
        // get the scores
        for (int index = 0; index < size; index++) {
            System.out.println("Enter golf score number " + (index + 1) + ":");
            Scanner keyboard = new Scanner(System.in);
            result.add(keyboard.nextInt());
        }
        return result;
    }
    public static void insertionSort(LinkedList<Integer> array) {
         //local variables
         java.util.Collections.sort(array, new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }

        });
    }

    public static void displayScores(LinkedList<Integer> scores) {
        //display the scores
        System.out.println("here are the scores: ");
        for (Integer score: scores) {
            System.out.println(score);
        }
    }
}

You have not incremented index in the for loop of getScores() method instead you are decreasing the size , so it wont read the elements into scores array properly always score[0] only gets the value and hence your reading values is not proerly added to array.

so better change it from for (index = 0; index <=size; size--) to for (index = 0; index <size; index++) see below working program and let me know for any help

public class SortedGolfScoresIB
{
     public static void main(String[] args) {
         final int SIZE = 10;
            int[] scores = new int[SIZE];
            int[] input =getScores(scores, SIZE);
            insertionSort(input);
        }
     public static int[]  getScores(int scores[], int size)
        {
            // local variable for loop index
            int index;


            // get the scores
            for (index = 0; index <size; index++)
            {
                System.out.println("Enter golf score number " + index +1 + ":");
                Scanner keyboard = new Scanner(System.in);
                scores[index] = keyboard.nextInt();
            }
            return scores;

            }
        private static void printNumbers(int[] input) {

            for (int i = 0; i < input.length; i++) {
                System.out.print(input[i] + ", ");
            }
            System.out.println("\n");
        }

        public static void insertionSort(int array[]) {
            int n = array.length;
            for (int j = 1; j < n; j++) {
                int key = array[j];
                int i = j-1;
                while ( (i > -1) && ( array [i] > key ) ) {
                    array [i+1] = array [i];
                    i--;
                }
                array[i+1] = key;

            }
            printNumbers(array);
        }
}

Reading the question, it splits naturally into four parts:

  • ask the user to enter ten golf scores.

  • store the ten scores in an integer array.

  • sort the array into ascending order.

  • display the contents of the sorted array.

You have written short pieces of code to perform those four separate tasks. What you need to do next is to check that you are getting what you expect between each of the four code-fragments by printing out the current state of your data at each transition. Look at the intermediate data, and compare it with what you expect to see. That will tell you where your code is failing.

First try to fix any parts that fail for yourself. If you are still stuck, post the failing code here and tell us what you have already tried in order to fix it. Otherwise, you are going to get some, "Have you tried..." suggestions where you reply, "Yes, I already tried that."

The more work you show that you have done the more likely we are to help you with your problem.

i got code to work how i wanted! here is the final code

import java.util.Scanner;

public class SortedGolfScoresIB
{
    public static void main(String[] args)
    {
        //local variables
        final int SIZE = 10;
        int[] scores = new int[SIZE];

        // get the scores
        getScores(scores, SIZE);

        // sore the scores in ascending order
        insertionSort(scores, SIZE);

        //display the results in ascending order
        displayScores(scores, SIZE);
    }

    // the getScoresmodule prompts the user for
    // golf scores to populare the scores array.

    public static void getScores(int scores[], int size)
    {
        // local variable for loop index
        int index;


        // get the scores
        for (index = 0; index < size; index++)
        {
            System.out.println("Enter golf score number " + (index +1) + ":");
            Scanner keyboard = new Scanner(System.in);
            scores[index] = keyboard.nextInt();
        }

        }
        // the insertionSort module sorts the contents of the  array
        // in ascending order
        public static void insertionSort(int  array[], int size)
        {
            //local variables
            int index;
            int scan;
            int unsortedValue;

            for (index = 1; index < size; index++)
            {
                unsortedValue = array[index];
                scan = index;

                array[scan] = unsortedValue;



            while (scan > 0 && array[scan-1] < array [scan])
                {
                swap(array[scan-1], array[scan]);
                scan = scan -1;
            }
        }

        }

        //the swap module swaps the contents of its two arguments
        public static void swap(int a, int b)
        {
            int temp;
            //swap a and b
            temp = a;
            a = b;
            b = temp;
        }

        // the display scores module displays the
        // golve scores in the scores array

        public static void displayScores(int scores[], int size)
        {

            // local variable for loop index
            int index;

            //display the scores
            System.out.println ("here are the scores: ");

            for (index=0; index < size; index++)
            {
                System.out.println(scores[index]);
            }
        }
    }

thanks everyone for the help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top