gpt4 book ai didi

java - 我应该如何用Java解析这个?

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

我无法理解如何解析具有未知数量“学生”的文本文档。我的所有解决方案都很奇怪,而且我在使用扫描仪时遇到了问题。分解输入,第一个整数代表有多少个类(class),第一个字符串是类(class)名称,下面是学生的相应日期和需要与学生一起存储的变量,学生数量未知。我想存储每个学生以及他们所在的类(class)。

到目前为止,我的代码非常困惑和困惑:

    String filename = "input.txt";
File file = new File(filename);
Scanner sc = new Scanner(file);
Student[] studArr = new Student[100];
int studCounter = 0;
boolean breaker = false;
boolean firstRun = true;
int numClasses = sc.nextInt();
System.out.println(numClasses);


while(sc.hasNextLine()){
String className = sc.nextLine();
System.out.println("Name: " + className);
String test = null;
breaker = false;
sc.nextLine();

// Breaks the while loop when a new class is found
while (breaker == false){
Student temp = null;

// Boolean to tell when the first run of the loop
if (firstRun == true){
temp.name = sc.nextLine();
}

else
temp.name = test;

System.out.println(temp.name);

temp.date = sc.nextLine();

if (temp.date.isEmpty()){
System.out.println("shit is empty yo");
}

temp.timeSpent = sc.nextInt();
temp.videosWatched = sc.nextInt();
temp.className = className;
studArr[studCounter] = temp;
studCounter++;
sc.nextLine();
test = sc.nextLine();
firstRun = false;
}
}
}
}

class Student {
public String name;
public String date;
public String className;
public int timeSpent;
public int videosWatched;
}

我不需要确切的答案,但我应该寻找与扫描仪不同的工具吗?有什么方法可以研究吗?

感谢您的帮助。

最佳答案

我想出了以下解决方案。扫描仪是完成这项工作的好工具。棘手的部分是,您必须提前查看是否有空行或日期,才能知道是否有学生或类(class)。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Parser {

private static String nextLine(Scanner sc) {
String line;
while (sc.hasNext()) {
if (!(line = sc.nextLine()).isEmpty()) {
return line;
}
}
return null;
}

public static ArrayList<Student>[] parseFile(String fileName) {
File file = new File(fileName);
try (Scanner sc = new Scanner(file)) {
int numClasses = sc.nextInt();
String className = nextLine(sc);
ArrayList<Student>[] classList = new ArrayList[numClasses];
for (int i = 0; i < numClasses; i++) {
classList[i] = new ArrayList<>();
while (true) {
String studentOrClassName = nextLine(sc);
if (studentOrClassName == null) {
break;
}
String dateOrBlankLine = sc.nextLine();
if (dateOrBlankLine.isEmpty()) {
className = studentOrClassName;
break;
}
int timeSpent = sc.nextInt();
int videosWatched = sc.nextInt();
classList[i].add(new Student(className, dateOrBlankLine, studentOrClassName, timeSpent,
videosWatched));
}
}
return classList;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return new ArrayList[0];
}

public static void main(String[] args) {
for (ArrayList<Student> students : parseFile("classList.txt")) {
if (!students.isEmpty()) {
System.out.println(students.get(0).className);
}
for (Student student : students) {
System.out.println(student);
}
}
}

static class Student {

public String className;
public String date;
public String name;
public int timeSpent;
public int videosWatched;


public Student(String className, String date, String name, int timeSpent,
int videosWatched) {
this.className = className;
this.date = date;
this.name = name;
this.timeSpent = timeSpent;
this.videosWatched = videosWatched;
}

public String toString() {
return name + '\n' + date + '\n' + timeSpent + '\n' + videosWatched + '\n';
}
}
}

关于java - 我应该如何用Java解析这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26328868/

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