Java 奇怪的file.nextLine()行为

Java 奇怪的file.nextLine()行为,java,file-io,Java,File Io,好的,我遇到了一个奇怪的问题,我试图得到一个文件,为几个类提供输入。这里是方法,其中大部分被注释掉了。在指定点,我需要输入一个多字字符串。但是,当.next()工作时,.nextLine()失败 java新手,如果这是显而易见的,那么很抱歉 public void readWeeklyData (String fileName) { try{ File file = new File(fileName); Scanner fileReader = new S

好的,我遇到了一个奇怪的问题,我试图得到一个文件,为几个类提供输入。这里是方法,其中大部分被注释掉了。在指定点,我需要输入一个多字字符串。但是,当.next()工作时,.nextLine()失败

java新手,如果这是显而易见的,那么很抱歉

public void readWeeklyData (String fileName) 
{
  try{
        File file = new File(fileName);
        Scanner fileReader = new Scanner(file);

        _leagueName = fileReader.nextLine(); //Outputs String properly despite spaces
        _leagueID = fileReader.nextInt();
        _numTeams = fileReader.nextInt();
       _numWeek = fileReader.nextInt();

      int i = 0;
      int j = 0;

       // while(i < _numTeams)
       // {
            Team team = new Team();
            team.setTeamID(fileReader.nextInt());
            System.out.println(team.getTeamID()); //Outputs 1011 properly
            team.setTeamName(fileReader.nextLine()); //Outputs nothing, with or without the while loops in comments. Yet when changed to fileReader.next(), it gives the first word of the team name.
           // team.setGamesWon(fileReader.nextInt());
          //  team.setGamesLost(fileReader.nextInt());
          //  team.setRank(fileReader.nextInt());

          // while(j < 3)
          //  {
           //     Bowler bowler = new Bowler();
         //       bowler.setBowlerId(fileReader.nextInt());
           //     bowler.setFirstName(fileReader.nextLine());
           //     bowler.setLastName(fileReader.nextLine());
          //      bowler.setTotalGames(fileReader.nextInt());
           //     bowler.setTotalPins(fileReader.nextInt());

           //     team.setBowler(j, bowler);
           //     j++;
           // }   

           // j = 0;

          //  _teams[i] = team; 
          //  i++;
        //}

   }

   catch(IOException ex)
    {
        System.out.println("File not found... ");
   }
}
由于
Scanner#nextInt
不使用换行符,因此紧跟在
1001
之后的换行符将传递给
nextLine
语句,您的团队名称将显示为空

在阅读下一行之前,您需要使用此字符。您可以使用:

fileReader.nextLine(); // consume newline
team.setTeamName(fileReader.nextLine());

周五晚间罢工27408 4 0 1001梦幻四人0 0 0 10011约翰尼·布莱克0 0 10012唐老鸭0 0 10013橄榄油0 0 10014达菲鸭0 0 1002展示船0 0 0 10021沃尔特·布朗0 0 10022蒂埃里森0 0 0 10023格雷戈里·拉森0 0 10024莎伦·尼利0 0 1003高阶滚轴0 0 0 10031马尔科姆·霍姆斯0 0 10032韦纳·弗兰克0 0 10033塞缪尔亚当斯0 0 10034贝蒂·克罗克0 0 1004炽热的保龄球手0 0 0 10041贾斯汀·贝茨0 0 10042汉娜·斯托姆0 0 10043脂肪多米诺骨牌0 0 10044吉米·亨德里克斯0 0Uh。。。进门的地方出了问题。我的错。我应该把文件上传到别处吗?是的。干得好。不过,我已经尽力了。@Xenosth为什么不将其格式化为代码或预标记?相关问题:
fileReader.nextLine(); // consume newline
team.setTeamName(fileReader.nextLine());