我有两个班Student
和Students
如何将文件读入student
数组列表以创建student
对象,而无需使用变量来保存下一行并将其转换为所需的数据类型(即String
到int
)。
public class Student
{
private String name;
private int age;
private double gpa;
public Student(String person, int years, double avg)
{
// initialise instance variables
name = person;
age = years;
gpa = avg;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public double getGPA()
{
return gpa;
}
public class Students
{
private ArrayList<Student>students;
public Students()
{
// initialise instance variables
students = new ArrayList<Student>();
}
public void add(Student s)
{
students.add(s);
}
public Student readFile() throws IOException
{
// reads data file into ArrayList
String line;
Scanner sc = new Scanner(new File("Students.txt"));
while (sc.hasNextLine()) {
**//code to read file into student array list**
}
sc.close();
}
我要读取的文件
Name0
22
1.2
Name1
22
2.71
Name2
19
3.51
Name3
18
3.91
请不要将其标记为重复或类似的问题。我已经在广泛搜索中寻找与我要解决的问题类似的已回答问题,但没有找到对我有用的问题。
答案 0 :(得分:0)
要从文件中获取字符串,可以调用Scanner.nextString():由于您的扫描器对象称为sc,因此看起来像sc.nextString()。要获取一个int,可以调用Scanner.nextInt(),而要获取一个double,可以调用Scanner.nextDouble()。
您不想将它们存储在中间值中,而是想立即创建一个学生值。您可以将所需的任何内容放入Student构造函数中,只要您输入的第一项内容为字符串,第二项内容为int,第三项内容为double。由于您的文件始终包含一个String,然后是一个int然后是一个double,我认为您可以使用上面列出的方法,并调用Student构造函数,以获取Student值。
答案 1 :(得分:0)
以这种方式尝试
public Student readFile() throws IOException
{
// reads data file into ArrayList
String line;
Scanner sc = new Scanner(new File("Students.txt"));
while (sc.hasNextLine()) {
Student student = new Student(sc.nextLine(), Integer.parseInt(sc.nextLine()), Double.parseDouble(sc.nextLine()));
}
sc.close();
}
答案 2 :(得分:-1)
import java.util.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
public class Students
{
public static void main(String[] args){
List<Student> students = new ArrayList<Student>();
File file = new File("C:\\Users\\Nik\\Desktop\\test.txt"); //Update path
int i = 0;
BufferedReader br;
try {
br = new BufferedReader(new FileReader(file));
String line;
Student student = new Student();
while ((line = br.readLine()) != null) {
if(i%3 == 0){
student = new Student();
student.setName(line);
} else if (i%3 == 1){
if(line != null && !line.isEmpty() && isNumber(line)){
student.setAge(Integer.parseInt(line));
}
} else if(i%3 == 2){
if(line != null && !line.isEmpty() && isNumber(line)){
student.setGpa(Double.parseDouble(line));
}
students.add(student);
}
i++;
}
for(Student x:students){
System.out.println(x.toString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static boolean isNumber(String s){
return s != null && s.matches("[-+]?\\d*\\.?\\d+");
}
}
class Student
{
private String name;
private int age;
private double gpa;
public Student(String person, int years, double avg)
{
// initialise instance variables
name = person;
age = years;
gpa = avg;
}
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", gpa=" + gpa + "]";
}
}