0

This is my list of Players and their Scores that I need to input from a text file, sort alphabetically, and then output to a new file. I then have to take the same list and sort it based on the average score at the end of each line and then output that sorted list to a file where the players are sorted by their scores. This is the list of players:

Bruce 127

Elayna 144

Lisa 153

Marcus 188

Amber 133

Ryan 149

Dorian 099

Joel 113

Kelly 097

This is my code so far. There are some methods and lines that are totally pointless that I haven't cleared out yet because I'm not sure if I need them or not.

public class MembersAndScores 
{

    //trying to create a method to read the input file, examine the first character in each line, and sort accordingly to get an alphabetically ordered list

    public static void insert(int[] x, int i)
    {
        int temp = x[i];
        int j = i - 1;
        while (j >= 0 && temp <x[j])
        {
            x[j+1] = x[j];
            j--;
        }
        x[j+1] = temp;
    }

    //Uses the insert method above by calling it to sort the array of strings below that hold the names and scores of the bowlers.
    public static void insertionsort(int[] x)
    {
        for (int i = 1; i < 9; i++)
            insert(x, i);
    }


    public static void main(String[] args) throws IOException
    {
        File inputFile = new File("players_scores.txt");
        if (!inputFile.exists())
        {
            System.out.println("File players_scores.txt was not found.");
            System.exit(0);
        }
        //setting up Scanner to read the input File, players_scores.txt. Created a String array to store the lines of text in.
        Scanner input = new Scanner(inputFile);
        String line;
        String[] array1 = new String[9];

        for (int i = 0; i <9; i++)
        {
            line = input.nextLine();
            array1[i]= line;
        }
        //calling my sorting method to sort the lines stored inside array1[0] through array1[8].
        Arrays.sort(array1);

        //begins the process of creating the file where data is sorted by name. program checks for txt file and creates it if missing.  
        File sortName = new File("sortedbyname.txt");
        PrintWriter output = new PrintWriter(sortName);

        //checks to see if the file exists and creates it if it doesn't
        if(!sortName.exists())
        {
            sortName.createNewFile();
        }

        for (int i = 0; i < 9; i++)
        {
            output.println(array1[i]);
        }
        output.close(); 
        //close output so it finishes up and prints the stored lines to the document.


        //begins the process of creating the file where data is sorted by score
        File sortScore = new File("sortedbyscore.txt"); 
        PrintWriter output2 = new PrintWriter(sortScore);
        String line2;


        Arrays.sort(array1);

        //computer checks to see if it doesn't exist and creates it if it doesn't
        if(!sortScore.exists())
        {
            sortScore.createNewFile();
        }

        for (int i = 0; i < 9; i++)
        {
            output2.println(array1[i]);
        }

        output2.close();
        input.close();
    }

}
3
  • have you learnt about OOP (Object Oriented Programming)? If you have, then you can do it in this way: parse the input_file.txt and for every player (every line in the text file) create a Player object with its respective score. You should have a list of Player which then you can compare their names or scores. :) Commented Nov 19, 2013 at 1:29
  • 1
    Are you allowed to use the sort methods Java provides, or is this a class assignment where you have to write your own sort algorithm? I'm confused, because you have both an insertionsort method where you're trying to sort things in order, and then you call Arrays.sort, which calls the Java library method to do a sort--it doesn't call your own sort() method, but even if it did, I don't understand why you want to sort right after you've already done your insertionsort. In any case, I think we need to know whether using Java's sort is allowed. Commented Nov 19, 2013 at 1:31
  • I TOOK OUT THE UNUSED METHODS AT THE TOP AND THE INSERTIONSORT. I'm not allowed to use OOP at all. And I'm sorry about the confusing pieces. I was trying different things and got stuck. I can use Java methods to sort and the insert method and insertionsort methods were some vague hint my professor gave me on how to do it, but I don't see how to use it to sort the list by the scores Commented Nov 19, 2013 at 1:37

2 Answers 2

1

I see that you can sort the array1 using array1.sort() this will only sort the array alphabetically. If you want to sort by the average test score I would do something like

    int avgScore[] = new int[9]; //make the nine whatever value you need.
    for(int i=0; i<avgScore.length;i++){
         avgScore[i] = Integer.parseInt(array1[i].substring(array1[i].length-                       3,array1[i].length));
    }
    avgScore.sort();

Substring is a very useful method in java and could be used to solve at least on of your issues. You can read about it here: http://www.tutorialspoint.com/java/java_string_substring.htm

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

4 Comments

Your example is calling an issue at array.length-3 over the array piece not being able to be resolved to a variable. How would one fix something like that? Asking Eclipse to do it tends to just cause tons of wacky issues. I'll give your tutorial link a read and try some more.
yes that is because I have made a mistake, I forgot to give an index value. I have fixed it now
I lost the names, but I'll take it. You provided me with a great start to tweak and pick at to get everything exactly perfect. Thank you a ton for the help and info. I really appreciate it.
no problem, this will get you to lose the name, but you can use a similar method to extract and save the names.
0

You can create Comparator:

static class ComparatorByScore implements Comparator<String> {

    public int compare(String o1, String o2) {
        Integer first = Integer.parseInt(o1.split(" ")[1]);
        Integer second = Integer.parseInt(o1.split(" ")[1]);
        return first.compareTo(second);
    }
}

and then use it in your code:

Arrays.sort(array1, new ComparatorByScore());

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.