读者不会在我的程序中停止(java)

时间:2014-10-25 05:46:57

标签: java arrays class java.util.scanner

我仍然是java的新手,在我目前的课堂项目中,我们正在尝试将学生设置为教室,学生信息是从我们创建的文本文件中读取的,但我的读者程序在4行之后不会停止(这是结束了学生的信息)。它会读取所有这些内容并打印出来,然后再分析它们必须能够将它们放入正确的classRoom中。请记住我还是java的新手。

我也不确定我一直关注Exception in thread "main" java.util.NoSuchElementException: No line found

的错误

这是我的读者计划

import java.io.*;
import java.util.Scanner;
public class Student{

    private Scanner file;
    public String name;
    public String id;
    public String year;
    public String course;
    //open the file to read
    public void openFile(){
      try{
          file = new Scanner(new File("StudentFile.txt"));
      }
      catch(Exception e)
      {
          System.out.print("Could not Open File");
      }
    }
    //Read the file and Assign the Student information strings
    protected void readFileText(){
        if(file.hasNext())
        {          
           for(int n=0; n<4; n++)
           {
                name = file.nextLine();
                id = file.nextLine();
                year = file.nextLine();
                course = file.nextLine();
                System.out.print("\n" + name + "\n" + id + "\n" + year + "\n" + course + "\n");
           }
        }   
        else
        {
             name = null;
        }
    }
    //close the file 
    public void closeFile(){
        file.close();
    }


    //set information

    public Student(){
          this.name = name;
          this.id = id;
          this.year = year;
          this.course = course;
    }


    //Get the informaton to main method

    public String getName(){
      return name;
    }

    public String getId(){
      return id;
    }
    public String getYear(){
      return year;
    }
    public String getCourse(){
      return course;
    }

}

这是我的主要方法

    public class Registrar {

        public static void main(String[] args) {
          Student student = new Student();
          student.openFile();

          String namenull = " ";
          String classRoom[] = {"Washington W1000", "Washington W1100", "Washington W1200", "Washington W1300", "Washington W1400", "Washington W1500", "Kennedy K1000", "Kennedy K1100", "Kennedy K1200", "Kennedy K1300"};
          int maxCapacity = 25;
          int studentNum = 0;
          int num = 0;
          int n = 0;
          String currentClass = classRoom[0];

          while(namenull != null){      // to read the file and set students to a classroom.
             student.readFileText();
             String course = student.getCourse();
                while (n < 10){
                   if(currentClass == "Washington W1400"){

                      maxCapacity = 30;
                   }
                   else if(currentClass == "Kennedy 1000"){
                      maxCapacity = 25;
                   }
                   else if(currentClass == "Kennedy 1200"){
                      maxCapacity = 35;
                   }
                   int num1 = 1;
                   int num2 = 2;
                   int num3 = 3;
                   if(studentNum < maxCapacity && course == "Comp 182" && num < 10){
                            System.out.print("\nRegistered in Comp 182, in classroom " + currentClass);
                            studentNum++;
                    }
                    else{
                               while(num == num1 || num == num2 || num == num3 || num == num){
                                     num = num++;
                               }
                     }
                    if(studentNum < maxCapacity && course == "Comp 182 Lab" && num1 < 10){
                            currentClass = classRoom[num1];
                            System.out.print("\nRegistered in Comp 182 Lab, in classroom " + currentClass);
                            studentNum++;
                     }
                     else{
                              while(num1 == num || num1 == num2 || num1 == num3 || num1 == num1){
                                   num1 = num1++;
                              }
                     }
                     if(studentNum < maxCapacity && course == "Comp 101" && num2 < 10){
                            currentClass = classRoom[num2];
                            System.out.print("\nRegistered in Comp 101, in classroom " + currentClass);
                            studentNum++;
                            }
                    else{
                             while(num2 == num || num2 == num1 || num2 == num3 || num2 == num2){
                                    num2 = num2++;
                             }
                    }
                    if(studentNum < maxCapacity && course == "Comp 101 Lab" && num3 <10){
                            currentClass = classRoom[num3];
                            System.out.print("\nRegistered in Comp 101 Lab, in classroom " + currentClass);
                            studentNum++;
                    }
                    else{
                          while(num3 == num || num3 == num1 || num3 == num2 || num3 == num3){
                              num3 = num3++;
                          }
                    }    
                    currentClass = classRoom[num];
                 }
                 namenull = student.getName();
            }
            student.closeFile(); 
        }
}

如我所知,我的StudentFile.txt

Alfredo Dominguez
1205586453
Freshman
Comp 182
Stacy Flores
6584527256
Sophmore
Comp 182 Lab

1 个答案:

答案 0 :(得分:1)

如果没有找到任何行,

Scanner会抛出NoSuchElementException。所以用

包装每个file.nextLine();
if(file.hasNextLine()){

 file.nextLine();

}
相关问题