gpt4 book ai didi

java - 从 java 中的文件读取但输出跳过每隔一行

转载 作者:行者123 更新时间:2023-12-01 22:41:30 26 4
gpt4 key购买 nike

我正在尝试读取文件并以特定格式打印结果。当它打印时,它只打印所有其他条目。在 while 循环中,我尝试切换 if 语句并将 0 更改为 -1,然后 count++ 但它不起作用。

  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();
}

最佳答案

我认为你的问题就在这里:

while((line = br.readLine()) != null)
{
if(count == 0)
{
count++;
continue;
}
players.add(br.readLine());
}

即使您已经读过一行,您每次都会阅读新行。你想要这个:

while((line = br.readLine()) != null)
{
if(count == 0)
{
count++;
continue;
}
players.add(line); //The important change is here.
}

关于java - 从 java 中的文件读取但输出跳过每隔一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26076322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com