这是我需要从文本文件输入的玩家及其分数列表,按字母顺序排序,然后输出到新文件。然后我必须采用相同的列表并根据每行末尾的平均分数对其进行排序,然后将排序后的列表输出到文件中,其中玩家按其分数排序。这是球员名单:
布鲁斯127
Elayna 144
Lisa 153
马库斯188
Amber 133
Ryan 149
Dorian 099
Joel 113
凯莉097到目前为止,这是我的代码。有些方法和线条完全没有意义,我还没有清除,因为我不确定我是否需要它们。
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();
}
}
答案 0 :(得分:1)
我看到你可以使用array1.sort()对array1进行排序,这只会按字母顺序对数组进行排序。如果你想按平均测试分数排序,我会做类似
的事情 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();
子串是java中非常有用的方法,可用于至少解决您的问题。您可以在此处阅读:http://www.tutorialspoint.com/java/java_string_substring.htm
答案 1 :(得分:0)
您可以创建比较器:
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);
}
}
然后在您的代码中使用它:
Arrays.sort(array1, new ComparatorByScore());