Input file looks like this

BASKETBALL
John Smith
23 , 54
FOOTBALL
Jose Cruise
445, 23
BASKETBALL
KJ
34, 32
BASEBALL
Mike Beterman
3, 2
FOOTBALL
David Smith
323, 23

Below is my code.

What is happening on my output I get


Sport: BASKETBALL Name: John Smith Stat: 23 , 54
Sport: FOOTBALL Name: Jose Cruise Stat: 445, 23
Sport: BASEBALL Name: Mike Beterman Stat: 323, 23

I need all the names and all the stats to be printed out..

Sport: BASKETBALL Name: John Smith Stat: 23 , 54
Sport: FOOTBALL Name: Jose Cruise Stat: 445, 23
Sport: BASKETBALL Name: KJ Stat: 34, 32
Sport: BASEBALL Name: Mike Beterman Stat: 3, 2
Sport: FOOTBALL Name: David Smith Stat:323, 23

Can someone tell me what I am doing wrong in my code Please?

BufferedReader in = new BufferedReader(new FileReader(infile));
  
  String line = "not empty";
  
  while (line != null)
   {
    

        if (line.equals("BASKETBALL"))
             
     {
       
         String Name = in.readLine(); 
         String stats= in.readLine(); 
        AddBasketballPlayer("Basketball", Name, Stats); 
         line = in.readLine(); 
     }
     if(line.equals(""BASEBALL"))
     {
         String Name = in.readLine(); 
         String stats= in.readLine(); 
         AddBaseballPlayer("Baseball", Name, Stats); 
         line = in.readLine(); 
     }
    if(line.equals("FOOTBALL"))
     {
         String Name = in.readLine(); 
         String stats= in.readLine(); 
         AddFootballPlayer("Football", Name, Stats); 
         line = in.readLine(); 
     }
  }

In line 18, you have too many double quotation?

if(line.equals(""BASEBALL"))

I just copied it incorrectly other than that is there a reason why it seems to skip over the BASKETBALL?

Because you are using "if" statement wrong. There are 2 ways to fix this..

// 1st way, move the line = in.readLine(); out from all of the "if"
while (line != null) {
  if (line.equals("BASKETBALL")) {
    String Name = in.readLine(); 
    String stats= in.readLine(); 
    AddBasketballPlayer("Basketball", Name, Stats); 
  }
  if(line.equals("BASEBALL")) {
    String Name = in.readLine(); 
    String stats= in.readLine(); 
    AddBaseballPlayer("Baseball", Name, Stats); 
  }
  if(line.equals("FOOTBALL")) {
    String Name = in.readLine(); 
    String stats= in.readLine(); 
    AddFootballPlayer("Football", Name, Stats); 
  }
  line = in.readLine(); 
}


// 2nd way which is much better, change your "if" only to "if" & "else if" statement
// The condition is mutual exclusive -- sport will never be the same, so you need
// the code to execute only one type in a loop.
while (line != null) {
  if (line.equals("BASKETBALL")) {
    String Name = in.readLine(); 
    String stats= in.readLine(); 
    AddBasketballPlayer("Basketball", Name, Stats); 
  }
  else if(line.equals("BASEBALL")) {
    String Name = in.readLine(); 
    String stats= in.readLine(); 
    AddBaseballPlayer("Baseball", Name, Stats); 
  }
  else if(line.equals("FOOTBALL")) {
    String Name = in.readLine(); 
    String stats= in.readLine(); 
    AddFootballPlayer("Football", Name, Stats); 
  }
  line = in.readLine(); 
}

Edited 5 Years Ago by Taywin: n/a

okay so simple, thanks one more question. My output is producing

Sport: BASKETBALL Name: John Smith Stat: 23 , 54
Sport: FOOTBALL Name: Jose Cruise Stat: 445, 23
Sport: BASKETBALL Name: KJ Stat: 34, 32
Sport: BASEBALL Name: Mike Beterman Stat: 3, 2
Sport: FOOTBALL Name: David Smith Stat:323, 23
nullnullnullnullnullnullnunullnullnullnullnullnullnullnullnullnullnullnullllnullnullnullnullnullnullnullnullnullnullnull

why is this null there shouldn't it end the loop as soon as it hits the empty line

The null value could come from your predefined array size. I don't know how you define the array to accept the add player functionality. If, for example, you create a predefined array as

SportPlayer[] players = new SportPlayer[40];

and you add up to 10 players, then the rest of your array will be null. When you display the result, which again I do not know how you display it, and may attempt to display all values inside the "players" array, you will get "null" value instead of what you want.

This article has been dead for over six months. Start a new discussion instead.