私は入力ファイルから配列にデータをプルするプログラムに取り組んでいます。私はデータが適切に実行される時点ですが、特定の順序で配列をソートする際に構文エラーが発生しています。入力ファイルは、次のデータを持っているとして使用されているテキストファイル:選択配列の並べ替え
1 2 2 2
19 0 5 1
2 0 0 6
18 4 2 0
4 1 2 3
12 2 2 2
7 0 0 3
8 1 4 1
10 2 2 2
3 2 1 3
11 6 0 0
2 0 5 1
19 0 0 6
17 4 2 0
9 3 2 1
4 2 1 3
3 1 2 3
7 0 0 3
最初の列は、プレイヤーの数であり、上記数値は、第三、第二の列は、そのプレーヤーのヒットで、野球チームのデータを表します列はそのプレイヤーのための散歩であり、第4列はそのプレイヤーのためのアウトです。
は以下である配列内に保存されているクラスがあります:
public class Player {
private int Number;
private int Hits;
private int Walks;
private int Outs;
Player() {
Number = Hits = Walks = Outs = 0;
}
public int getNumber() {
return Number;
}
public int getHits() {
return Hits;
}
public int getWalks() {
return Walks;
}
public int getOuts() {
return Outs;
}
public void setNumber(int n) {
Number = n;
}
public void setHits(int h) {
Hits = h;
}
public void setWalks(int w) {
Walks = w;
}
public void setOuts(int o) {
Outs = o;
}
// overload for output
public String toString() {
String s = new String();
s = String.format("%2d",Number) + "\t" +
String.format("%2d",Hits) + "\t" +
String.format("%2d",Walks) + "\t" +
String.format("%2d",Outs);
return s;
}
} // end of the class
最後に、これは私のプログラムのコードです。私はそれがプレーヤーの番号で配列をソートすることになると、構文エラーを取得しています。プレイヤー番号は昇順でなければならない唯一の列です。アドバイスをいただければ幸いです。
import java.io.*;
import java.util.*;
// declaration of the class
public class Baseball9 {
// implementation of main program
public static void main(String[] args) throws FileNotFoundException {
// 1) connect to input file
Scanner fin = new Scanner(new FileReader("baseball.txt"));
// objects used to store data
final int LIST_LENGTH = 20;
int number = 0, // number, hits, walks, outs
hits,
walks,
outs,
players,
index,
teamSize = 0;
// 2) output descriptive messages
System.out.println("This program tracks a baseball player's number "
+ "and their\nnumber of walks, runs and outs for "
+ "each game in a season.\n");
// 3) declare an array of LIST_LENGTH players
Player[] team = new Player[LIST_LENGTH];
// 3a) loop over the teamsize
for (int i = 0; i < LIST_LENGTH; i++) {
// 3b) instantiate the i'th team member
team[i] = new Player();
}
// 4) loop on end of file
while (fin.hasNext()) {
// 5) attempt to input the data for the next Player
number = fin.nextInt();
hits = fin.nextInt();
walks = fin.nextInt();
outs = fin.nextInt();
// 6) find the index of this Player's number
int pos = findNumber(team, LIST_LENGTH, number);
// 7) if player number is not in the list
if (pos < 0) {
// 7a) set the Number field for team[teamSize]
team[teamSize].setNumber(number);
// 7b) set the Hits field for team[teamSize]
team[teamSize].setHits(hits);
// 7c) set the Walks field for team[teamSize]
team[teamSize].setWalks(walks);
// 7d) set the Outs filed for team[teamSize]
team[teamSize].setOuts(outs);
// 7e) increase teamSize by 1
teamSize++;
}
// 8) else player number is in the list
else {
// 8a) update the Hits field for team[index]
team[teamSize].setHits(hits + team[teamSize].getHits());
// 8b) update the Walks field for team[index]
team[teamSize].setWalks(walks + team[teamSize].getWalks());
// 8c) update the Outs field for team[index]
team[teamSize].setOuts(outs + team[teamSize].getOuts());
}
} // end while
// 9) display the results
displayArray(team, teamSize);
selectionSort(team, teamSize);
displayArray(team, teamSize);
// 10) disconnect from input file
fin.close();
} // end of main
// *************************************************************************
// determine the position within an array of Player of a player's index number
// performs a sequential search from the begining of the array
// returns -1 on a failure to find an index within the array
//**************************************************************************
// 1) assume that this player_ number is not in the list
public static int findNumber(Player list[], int listLength, int searchItem) {
int loc;
boolean found = false;
loc = 0;
// 2) loop over the list length
while (loc < listLength && !found)
// 3) exit the loop if the number is found
if (list[loc].getNumber() == searchItem)
found = true;
// 4) update the index on successful search
else
loc++;
// 5) return either the found index or -1
if (found)
return loc;
else
return -1;
}
//************************************************************************
// display players in an array of Player with formatting of
// Player #, Number of Hits, Number of Walks, Number of Outs
//************************************************************************
public static void displayArray(Player list[], int team_size) {
// 1) display headins of colums
System.out.println("\n\nPlayer\tHits\tWalks\tOuts\n"
+ "------\t----\t-----\t----\n");
// 2) loop over team size
for (int i=0; i < team_size; i++) {
// 3) display i'th player
System.out.println(list[i]);
}
}
public static void selectionSort(Player list[], int listLength) {
int index;
int smallestIndex;
int minIndex;
int temp;
for (index = 0; index < listLength - 1; index++) {
smallestIndex = index;
for (minIndex = index + 1; minIndex < listLength; minIndex++)
if (list[minIndex] < list[smallestIndex])
smallestIndex = minIndex;
temp = list[smallestIndex];
list[smallestIndex] = list[index];
list[index] = temp;
}
}
} // end of the class
あなたの質問を編集し、完全な入力出力とエラーログを追加してください。 –
'Player'クラスでは、すべての変数の最初の文字を小文字にするのが最善です。 'Number'は' java.lang'のクラス名です。 'fin.hasNextInt()'も使わなければなりません。あなたの質問を編集して、エラーの内容と出ている行を表示してください。 – 4castle
'Player'はクラスです。デフォルトの' sort'メソッドを使ってクラスをソートすることはできません。代わりに独自の 'Comparator'を作成してください:http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property – Meinkraft