0

Okay, so I'm having this weird issue where I'm trying to get a File to give input into several classes. Here is the method, most of which is commented out. At the specified point, I'm needed a multi-word string put in. However, while .next() works, .nextLine() fails.

New to java, so sorry if this is obvious.

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... ");
   }
}

The file (Ignore the extra enters, as they were only there for formatting. This is only 2 iterations of teams.):

Friday Night Strikers
27408
4
0
1001
Fantastic Four
0
0
0
10011
Johnny
Blake
0
0
10012
Donald
Duck
0
0
10013
Olive
Oil
0
0
10014
Daffy
Duck
0
0
1002
The Showboats
0
0
0
10021
Walter 
Brown
0
0
10022
Ty
Ellison
0
0
10023
Gregory
Larson
0
0
10024
Sharon
Neely
0
0
Reimeus
  • 158,255
  • 15
  • 216
  • 276
Xenorosth
  • 97
  • 1
  • 9
  • Friday Night Strikers 27408 4 0 1001 Fantastic Four 0 0 0 10011 Johnny Blake 0 0 10012 Donald Duck 0 0 10013 Olive Oil 0 0 10014 Daffy Duck 0 0 1002 The Showboats 0 0 0 10021 Walter Brown 0 0 10022 Ty Ellison 0 0 10023 Gregory Larson 0 0 10024 Sharon Neely 0 0 1003 High Rollers 0 0 0 10031 Malcolm Holmes 0 0 10032 Verna Frank 0 0 10033 Samuel Adams 0 0 10034 Betty Crocker 0 0 1004 Blazing Bowlers 0 0 0 10041 Justin Bates 0 0 10042 Hannah Storm 0 0 10043 Fats Domino 0 0 10044 Jimi Hendrix 0 0 – Xenorosth Feb 27 '13 at 03:09
  • Uh... The enters didn't come out right. My bad. Should I upload the file elsewhere? – Xenorosth Feb 27 '13 at 03:09
  • Yeah. Here you go. Best I could do, though. – Xenorosth Feb 27 '13 at 03:19
  • @Xenorosth Why wouldn't you just format it as code or in pre tags? – Dave Newton Feb 27 '13 at 03:21
  • ***Related Question:*** [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/q/7056749/597657) – Eng.Fouad Feb 27 '13 at 03:23

1 Answers1

1

Because Scanner#nextInt doesn't consume newline characters, the newline character immediately after 1001 will be passed though to the nextLine statement and your team name will appear empty.

You need consume this character before reading the next line. You can use:

fileReader.nextLine(); // consume newline
team.setTeamName(fileReader.nextLine());
Reimeus
  • 158,255
  • 15
  • 216
  • 276