1

I'm having trouble understanding how to parse text documents with unknown amounts of 'students'. All my solutions are coming up strange and I'm having trouble with the Scanner. Breaking down the input, the first integer represents how many classes there are, the first string is the class name, the following are students with respective dates and variables that need to be stored along with the student, with an unknown amount of students. I want to store each student along with the class they are in.

My code is extremely messy and confusing so far:

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

I don't need an exact answer, but should I be looking into a different tool then Scanner? Is there a method I can research?

Thanks for any assistance.

RJones
  • 85
  • 1
  • 9
  • 2
    You have problem described here http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-nextint. Also `if (firstRun == true)` is not best style of coding (it is very easy to make mistake like `if (firstRun = true)`). It is better to simply write `if (firstRun)` – Pshemo Oct 12 '14 at 19:08

2 Answers2

1

Ask yourself, what does a Student contain? A name, date, number and number. So you want to do the following (not actual code) (format written in Lua code, very understandable. This means this will not run in 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
engineercoding
  • 802
  • 6
  • 14
1

I came up with the following solution. Scanner is a fine tool for the job. The tricky part is that you have to sort of look ahead to see if you have a blank line or a date to know if you have a student or a 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';
    }
  }
}
nmore
  • 2,075
  • 1
  • 11
  • 18
  • Wow! Thank you so much. I'll look through this, but I can already tell it will teach me a lot. – RJones Oct 12 '14 at 23:22