gpt4 book ai didi

java - 使用 BufferedReader 和 Scanner 读取文本

转载 作者:行者123 更新时间:2023-11-30 11:20:03 25 4
gpt4 key购买 nike

我正在学习 Java 并使用 BlueJ,所以我的问题与我被要求完成的部分作业有关。其中一些有效,但我在其他部分遇到问题。我将包括到目前为止我在类里面所做的一切,但首先是要求:

  1. 提示用户选择合适的文件//完成并工作
  2. 使用 BufferedReader 和 Scanner 对象逐行读取文件//完成并假设工作正常!
  3. 当读取包含运行者姓名和年龄的每一行时,应创建一个新的 Runner 对象并将其实例变量设置如下://Class Runner already created //MathatonAdmin 类 - 当前类。

    • 3a) 名称 - 可以使用文件中的值直接设置。//使用 System.out.println 进行测试,所有值(在显示面板中)均显示 (*)。
    • 3b) ageGroup - 可以从给定的年龄开始计算:18 岁以下的运行者应归类为初级,55 岁及以上的为高级,其他均为标准。//使用 System.out.println 进行测试,所有值(在显示面板中)均显示 (*)。
    • 3c) 应将 Runner 的实例添加到实例变量 runners 引用的列表中。

基本上当我运行提供的测试代码时:

MarathonAdmin ma = new MarathonAdmin();
ma.readInRunners();

我应该在检查 ma 时看到运行者名单;目前列出了一个人的姓名和年龄。

所以我需要 3a - 3c 方面的帮助。如何使用所述变量创建一个新的 Runner 实例,然后将实例 Runner 添加到 runners 的列表中?

我已经在 while 循环中尝试了 for 循环,但是因为我在猜测 for 循环,所以我没有在变量 ma 中获得所需的列表。

我正在使用 System.out.println 来测试我至少拥有正确的文件。

如有任何帮助或建议,我们将不胜感激。

MarathonAdmin 类:

import java.util.*;
import java.io.*;
import ou.*;

/**
* MatharthonAdmin Class
*
* @author Stephen Berry
* @version 28/03/14
*/
public class MarathonAdmin
{
// instance variables
private String runners;
private String age;

/**
* Constructor for objects of class MarathonAdmin
*/
public void MarathonAdmin()
{
List<String> runners = new ArrayList<>();
}

public void readInRunners()
{
String pathName = OUFileChooser.getFilename();
File aFile = new File(pathName);
BufferedReader bufferedFileReader = null;

try
{
String currentLine;
Scanner lineScanner;
bufferedFileReader = new BufferedReader(new FileReader(aFile));
currentLine = bufferedFileReader.readLine();

while (currentLine != null)
{
lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");
runners = lineScanner.next();
age = lineScanner.next();

for (String aList: runners)
{
Runner runners = new Runner();

if (Integer.parseInt(age) < 18)
{
System.out.println(currentLine + " : Junior");
}

if (Integer.parseInt(age) > 55)
{
System.out.println(currentLine + " : Senior");
}

if (Integer.parseInt(age) > 18 && Integer.parseInt(age) < 55)
{
System.out.println(currentLine + " : Standard");
}

currentLine = bufferedFileReader.readLine();
}
}
}

catch (Exception anException)
{
System.out.println("Error: " + anException);
}

finally
{
try
{
bufferedFileReader.close();
}

catch (Exception anException)
{
System.out.println("Error: " + anException);
}
}
}

}

最佳答案

您的类(class)中有一些点导致您的程序出现这种错误行为。

  1. 练习说你应该创建一个运行者对象列表作为实例变量,对吗?查看您的马拉松类的实例变量 runners 并查看它的类型。 ;)

  2. 您的 while 循环是一个很好的方法。现在在 while 循环中,你对文本文件的每一行进行交互,这相当于一个运行者,对吗?那么为什么需要 for 循环呢?您可以使用 lineScanner 获取行的每个部分,不需要第二个循环我尝试为您提供一个带有伪代码的结构

字符串运行者姓名; 诠释 runnerAge;

while (currentLine != null)
{
lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");

runnerName = lineScanner.next();
runnerAge = lineScanner.next();

runners = lineScanner.next();
age = lineScanner.next();

create new Runner Object

set age and name of object according to the data you ve just read

runners.add(Runnerobject) // <-- this line adds the object you ve just created to the list which is your instancevariable. Make sure that this variable really is a list :)

currentLine = bufferedFileReader.readLine();
}
}

希望对您有所帮助。

关于java - 使用 BufferedReader 和 Scanner 读取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22938738/

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