我应该如何用Java解析它?

时间:2014-10-12 19:05:00

标签: java parsing

我无法理解如何解析未知数量的“学生”的文本文档。我所有的解决方案都很奇怪,我在扫描仪上遇到了麻烦。分解输入,第一个整数表示有多少个类,第一个字符串是类名,以下是具有相应日期和变量的学生,需要与学生一起存储,学生数量不明。我想把每个学生和他们所在的班级一起存放。

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

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

我不需要一个确切的答案,但我应该研究一个不同的工具,然后扫描仪?有没有可以研究的方法?

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

问问自己,学生包含什么?姓名,日期,号码和号码。所以你想要做以下(不是实际的代码)(用Lua代码编写的格式,非常容易理解。这意味着这不会在Lua中运行:P)

if line is not empty then
    if followingLine is date then
       parseStudent() // also skips the lines etc
    else
       parseClass() // also skips lines
   end
end

答案 1 :(得分:1)

我提出了以下解决方案。扫描仪是一个很好的工具。棘手的部分是你必须先看看你是否有一个空白行或一个日期来知道你是否有学生或班级。

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';
    }
  }
}