Reputation: 23
I am trying to read a file and print out the results in a specific format. When it prints, it only prints every other entry. In the while loop, I tried switching the if statement around and changing 0 to -1 and then count++ but it didn't work.
try
{
File f = new File("BaseballNames1.csv");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
ArrayList<String> players = new ArrayList<String>();
String line;
int count = 0;
while((line = br.readLine()) != null)
{
if(count == 0)
{
count++;
continue;
}
players.add(br.readLine());
}
for(String p : players)
{
String[] player = new String[7];
player = p.split(",");
first = player[0].trim();
last = player[1].trim();
birthDay = Integer.parseInt(player[2].trim());
birthMonth = Integer.parseInt(player[3].trim());
birthYear = Integer.parseInt(player[4].trim());
weight = Integer.parseInt(player[5].trim());
height = Double.parseDouble(player[6].trim());
name = first + " " + last;
birthday = birthMonth + "/" + birthDay + "/" + birthYear;
System.out.println(name + "\t" + birthday + "\t" + weight + "\t" + height);
//System.out.printf("First & Last Name %3s Birthdate %3s Weight %3s Height\n", name, birthday, weight, height);
}
}
catch(Exception e)
{
e.getMessage();
}
Upvotes: 0
Views: 912
Reputation: 8386
Change
players.add(br.readLine());
to
players.add(line);
Your version reads and writes the next line to players
, not the current one.
Upvotes: 1
Reputation: 5046
I think your issue is right here:
while((line = br.readLine()) != null)
{
if(count == 0)
{
count++;
continue;
}
players.add(br.readLine());
}
You're reading a new line each time, even though you already read one. You want this:
while((line = br.readLine()) != null)
{
if(count == 0)
{
count++;
continue;
}
players.add(line); //The important change is here.
}
Upvotes: 2