有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVAlang.ArrayIndexOutofBounds字符串排名的异常=playerParts[0]

我在while循环中的第二个while循环中不断得到一个java.lang.ArrayIndexOutOfBoundsException。有没有我错过的一步?我正在从文件中读取我的信息。本课程的全部目标是让我做到以下几点:

  1. 提示用户输入文件路径
  2. 提示用户知道应该创建多少个团队
  3. 向用户询问每个团队的名称(如果愿意,您仍然可以在内部为团队使用数字/索引,但您需要能够在最后输出带有名称的团队)
  4. 对于选秀模式,每个团队应按顺序选秀,但应在交替轮次中颠倒顺序(即第一轮选秀的团队是第二轮选秀的最后一个团队,以此类推)
  5. 继续进行草稿,直到a)没有更多玩家/角色,或者b)在两轮之间提示用户是否要继续,并指示“否”(仅n就可以)
  6. 为球队选择球员/角色
    1. 向用户显示可用人员所处的不同职位/角色,以便选择一个(如果需要,可以使用数字输入来选择)
    2. 一旦用户选择了职位/角色,显示可用的玩家/角色供用户选择
    3. 一旦被选中,玩家/角色将被添加到相应的团队中
  7. 由于可用选项用尽或用户指示草稿已完成,草稿结束后,显示以下内容:
    1. 每个团队(团队名称、球员-价值信息、姓名、创造者/团队和团队总数(即团队中所有人的价值)
    2. 最后,指出总体价值最低的团队(即平均而言,拥有“最佳”员工的团队)

课程材料中提供了示例文件

格式如下所示,多个值的行上有制表符分隔的条目: QB<;-职位/角色
1 A.罗杰斯GB<;-价值、名称、团队/创建者
2 T.布雷迪NE
3 D.布里斯否
-----<;-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   );
    }

}

}

我将非常感谢您的帮助谢谢


共 (2) 个答案

  1. # 1 楼答案

    这一行:

    String[] playerParts = possiblePlayer.split("\t");
    

    无法按预期工作,因为在某些情况下possiblePlayer的选项卡少于3个(\t

    在访问数组playerParts之前,必须检查数组playerParts是否具有所需的元素数, 用户playerParts.length获取数组的元素数

  2. # 2 楼答案

    当你打电话的时候

    possiblePlayer.split("\t");
    

    你得到的结果不到3个

    您确定这些字段是用制表符分隔的吗

    您可以插入以下几行来确定实际发生了什么

    String[] playerParts = possiblePlayer.split("\t");
    System.out.println("possiblePlayer: " + possiblePlayer);
    System.out.println("num fields: " + playerParts.length);