Java 如何读取、处理文本文件&;写入toString()
我对此有很多问题: 1) 我想把扫描器放在哪个类下,以便它分配适当的变量?给我的任务是在Tester课堂上“将数据文件读入学生” 2) 如何在学生课堂上创建Java 如何读取、处理文本文件&;写入toString(),java,file-io,Java,File Io,我对此有很多问题: 1) 我想把扫描器放在哪个类下,以便它分配适当的变量?给我的任务是在Tester课堂上“将数据文件读入学生” 2) 如何在学生课堂上创建readFile()方法 3) 如何在学生和学生课堂上正确编写toString() import java.io.*; import java.util.*; public class Tester { public static void main(String args[]) throws IOException {
readFile()
方法
3) 如何在学生和学生课堂上正确编写toString()
import java.io.*;
import java.util.*;
public class Tester
{
public static void main(String args[]) throws IOException
{
//new Students object
Students theStudents = new Students();
//reads file from Students
theStudents.readFile();
// create new 'Output.txt' file to save program output
PrintWriter ot = new PrintWriter(new FileWriter("Output.txt"));
System.out.println(theStudents.toString());
ot.println(theStudents.toString());
ot.close();
}
}
import java.util.ArrayList;
import java.io.*;
import java.util.*;
import java.io.FileReader;
public class Students
{
// instance variables
private ArrayList<Student> students;
public Students()
{
//create arraylist for students
students = new ArrayList<>();
}
public void readFile() throws IOException
{
String name;
int age;
double gpa;
String line;
//Scanner to read file
try {
Scanner sc = new Scanner(new File("Students.txt"));
while (sc.hasNextLine()) {
name = sc.nextLine();
line = sc.nextLine();
age = Integer.parseInt(line);
line = sc.nextLine();
gpa = Double.parseDouble(line);
}
}
catch (FileNotFoundException e) {
System.out.println("Error");
}
}
public void add(Student s)
{
students.add(s);
}
public Students aboveAverage(double avgGPA)
{
Students aboveAverage = new Students();
for (int i = 0; i < students.size(); ++i) {
if (students.get(i).getGPA() > avgGPA)
aboveAverage.add(students.get(i));
}
return aboveAverage;
}
public String toString()
{
String out = "";
int count = 0;
for (Student student : students){
out += students.toString() + " ";
++count;
}
return out;
}
}
public class Student
{
private String name;
private int age;
private double gpa;
public Student(String studentName, int studentAge, double studentGPA)
{
name = studentName;
age = studentAge;
gpa = studentGPA;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public double getGPA()
{
return gpa;
}
public String toString()
{
return String.format("%10s", name) +
String.format("%5d", age) + String.format("%10.2f \n", gpa);
}
}
import java.io.*;
导入java.util.*;
公共类测试员
{
公共静态void main(字符串args[])引发IOException
{
//新生反对
学生学生=新生();
//从学生那里读文件
theStudents.readFile();
//创建新的“Output.txt”文件以保存程序输出
PrintWriter ot=新的PrintWriter(新的FileWriter(“Output.txt”);
System.out.println(theStudents.toString());
ot.println(theStudents.toString());
ot.close();
}
}
导入java.util.ArrayList;
导入java.io.*;
导入java.util.*;
导入java.io.FileReader;
公立班学生
{
//实例变量
私立ArrayList学生;
公立学生()
{
//为学生创建arraylist
学生=新数组列表();
}
public void readFile()引发IOException
{
字符串名;
智力年龄;
双gpa;
弦线;
//扫描仪读取文件
试一试{
Scanner sc=新扫描仪(新文件(“Students.txt”);
while(sc.hasNextLine()){
name=sc.nextLine();
line=sc.nextLine();
年龄=整数.parseInt(行);
line=sc.nextLine();
gpa=Double.parseDouble(行);
}
}
catch(filenotfounde异常){
System.out.println(“错误”);
}
}
公共无效添加(学生s)
{
学生。添加(s);
}
中等以上公立学生(平均双倍)
{
中等以上学生=新生();
for(int i=0;iavgGPA)
高于平均水平。添加(学生。获取(i));
}
回报高于平均水平;
}
公共字符串toString()
{
串出“”;
整数计数=0;
用于(学生:学生){
out+=students.toString()+“”;
++计数;
}
返回;
}
}
公立班学生
{
私有字符串名称;
私人互联网;
私人双gpa;
公立学生(String studentName、int studentAge、double studentGPA)
{
姓名=学生姓名;
年龄=学生年龄;
gpa=学生gpa;
}
公共字符串getName()
{
返回名称;
}
公共整数getAge()
{
回归年龄;
}
公共双getGPA()
{
返回gpa;
}
公共字符串toString()
{
返回字符串。格式(“%10s”,名称)+
String.format(“%5d”,age)+String.format(“%10.2f\n”,gpa);
}
}
我不会给你完整的解决方案,但这里有一个解决这个问题的方法
- 您需要
而不是readLine()
nextLine()
- 读取值后,需要使用Student对象调用
函数将其添加到ArrayList中add()
try (Scanner sc = new Scanner(new File("Students.txt"))) {
while (sc.hasNextLine()) {
String name = sc.readLine();
int age = Integer.parseInt(sc.readLine());
double gpa = Double.parseDouble(sc.readLine());
/* Create A New Student Object & Add To List */
add(new Student(name, age, gpa));
}
} catch (FileNotFoundException e) {
System.out.println("Error");
}
此外,您还需要覆盖toString()
函数