java.lang.ArrayIndexOutOfBoundsException
while ループ内で 2 番目の while ループを取得し続けます。欠けているステップはありますか?ファイルから情報を読み取っています。このプログラムの全体的な目標は、私が次のことを行うことです。
- ユーザーにファイル パスの入力を求めます。
- 作成する必要があるチームの数をユーザーに知らせます。
- ユーザーに各チーム名を尋ねます (内部的には、必要に応じてチームに番号/インデックスを使用できますが、最後に名前を付けてチームを出力できるようにする必要があります)。
- ドラフト パターンでは、各チームは順番にドラフトする必要がありますが、ラウンドごとに順序を逆にする必要があります (つまり、ラウンド 1 でドラフトする最初のチームがラウンド 2 でドラフトする最後のチームになる、など)。
- a) 残りのプレイヤー/キャラクターがなくなるか、b) ユーザーが続行したい場合はラウンドの合間にプロンプトが表示され、ユーザーが「いいえ」を示すまでドラフトに進みます (n だけで問題ありません)。
- チームのプレイヤー/キャラクターを選択するには
- 利用可能な人々がいるさまざまなポジション/ロールをユーザーに表示して、1つを選択できるようにします(必要に応じて、数値エントリを使用して選択できます)。
- ユーザーがポジション/役割を選択したら、利用可能なプレーヤー/キャラクターを表示して、ユーザーがいずれかを選択できるようにします
- 選択すると、プレーヤー/キャラクターが適切なチームに追加されます
- 利用可能な選択肢がなくなった、またはユーザーが完了を示したためにドラフトが終了したら、次のように表示します。
- 各チーム (チーム名、プレーヤー - 値、名前、作成者/チームの情報、およびチームの合計 (つまり、チームのすべての人の値)
- 最後に、全体の合計値が最も低いチームを示します (つまり、平均して「最高の」人材を擁するチーム)。
サンプル ファイルは、コース教材で入手できます。
形式は次のとおりで、複数の値を持つ行のエントリは
タブで区切られてい
ます
。- <- 5 つのハイフンは、ファイルが新しい位置/役割に切り替わろうとしていることを示します RB <- 次の位置/役割
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeMap;
public class Superhero {
static TreeMap<String, ArrayList<Player>> positions = new TreeMap<String, ArrayList<Player>>();
ArrayList<String> team = new ArrayList<String>();
public static void main(String[] args) throws IOException {
int teams = 0;
String fileName = "";
Scanner input = new Scanner(System.in);
System.out.print("Whats the name of the file: ");
fileName = input.next();
File superHeroFile = new File(fileName);
Scanner file = new Scanner(superHeroFile);
while (file.hasNextLine()){
//read the position
String role = file.nextLine();
// ready to create the ArrayList for all players in this role
ArrayList<Player> playersInRole = new ArrayList<Player>();
positions.put(role , playersInRole);
// until I read "-----" I have a new all players in this current position
String possiblePlayer = file.next();
while (!possiblePlayer.equals("-----")){
String[] playerParts = possiblePlayer.split("\t");
String ranking = playerParts[0];
String name = playerParts[1];
String originalTeam = playerParts[2];
}
}
for(int buildTheTeam = 0; buildTheTeam < teams; buildTheTeam++){
int playerType = input.nextInt();
switch (playerType){
case 1: //Leader
break;
case 2: //Brawn
break;
case 3: //Gadgets
break;
case 4: //Female Influence
break;
case 5: //Bad Guy
break;
}
}
while (file.hasNextLine()){
String wholeFile = file.nextLine();
System.out.println(wholeFile);
}
//get the number of teams
System.out.print("How many teams will you have? ");
teams = input.nextInt();
input.nextLine();
for(int teamName = 0; teamName < teams; teamName++){
System.out.print("What is the name of your team? ");
String TeamName = input.next();
System.out.println("Team " + TeamName );
}
}
}
助けていただければ幸いですありがとうございます!