为什么我不能从我的文本文件中读取我的文件?

时间:2017-05-11 12:15:47

标签: java

我想从文本文件中读取我的文件..但为什么我不能读它?任何人都可以为我检查?

这是我的子类代码:

 public class Student {
     private String name;
     private double practical,exam;
     private double[] assignment = new double[4];
     private int ID,assignmentno;


    public Student(){
        name = null;
        practical = 0;
        exam = 0;
        assignmentno=0;
        assignment[assignmentno] = 0;
        ID = 0;
    }

void setDetails(String stdname,int stdID){
    name = stdname;
    ID = stdID;
}
 void setPractical(double practicalscore){
    practical = practicalscore;
    if(practicalscore>10 || practicalscore<0){
        practical = 0;
    }
}
 void setAssignment(double assignmentscore,int assignmentnumber){
     assignmentno = assignmentnumber;
    assignment[assignmentno]=assignmentscore;
    if(assignment[assignmentno]>10 || assignment[assignmentno]<1){
        assignment[assignmentnumber] = 0;
    }
}
void setExam(double examscore){
    exam = examscore;
    if(examscore>100 || examscore<0){
        exam = 0;
    }
}
String getName(){
    return name;
}
int getID(){
    return ID;
}
double getPracticalMark(){
    return practical;
}
double getAsgMark(int a){
return assignment[a];
}
double getTotalAsgMark(){
    double totalassignment=0;
    for(int x=0;x<4;x++){
        totalassignment = totalassignment + assignment[x];
    }
    return totalassignment;
}
double getExamMark(){
    return exam;
}
double getOverallMark(){
  double overall;
  overall = practical + getTotalAsgMark() + (0.5*exam);
  return overall;
}

这是我的主要课程:

  import java.io.*;
  import java.util.*;
  public class driver {   
  public static void main(String args[])throws IOException
{
    int options;
    //creation of array
    Student [] a = new Student[30];
    Scanner read = new Scanner(System.in);
    //creation of objects
    for(int x = 0;x<30;x++)
    {
        a[x] = new Student();
    }

    do
    {

    System.out.println("Please choose an option: ");
    System.out.println("1. Read data from file.");
    System.out.println("2. Write data to file.");
    System.out.println("8.exit");
    switch(options)
    {
        case 1:
                ReadFile(a);
                break;
            case 2:
                WriteFile(a);
                break;
        }
    }
    while(options!=8);
  }

    public static void ReadFile(Student[] array)throws IOException
 {
     String abc;
     int size = 0;
     int stdID,assignmentnumber;
     double practicalscore,examscore,assignmentscore;
     Scanner inputFile = new Scanner(new BufferedReader(new 
        FileReader("1234.txt")));   
     int x = 0;

     while(inputFile.hasNext())
     {
      int counter = 0;
      abc = inputFile.next();
      stdID = inputFile.nextInt();
      array[x].setDetails(abc,stdID);
      practicalscore = inputFile.nextDouble();
      array[x].setPractical(practicalscore);
     do
     {
      assignmentnumber =inputFile.nextInt();
      assignmentscore =inputFile.nextDouble();
      array[x].setAssignment(assignmentscore,assignmentnumber);
      counter++;
      }
     while(counter!=4);
      examscore = inputFile.nextDouble();
      array[x].setExam(examscore);
      size ++;
      x++;
    }
    }
 public static void WriteFile(Student[]array) throws IOException
{
            FileWriter fw = new FileWriter("2234.txt",true);
            PrintWriter pw = new PrintWriter(fw);
            for(int y = 0; y<30;y++)
            {

            pw.println(array[y].getName());
            pw.println(array[y].getID());
            pw.println(array[y].getPracticalMark());
            for(int x = 0;x<4;x++)
            {
            pw.println(x);
            pw.println(array[y].getAsgMark(x));
            }
            pw.println(+array[y].getTotalAsgMark());
            pw.println(array[y].getExamMark());
            }                  
}

这是我从我的文件中运行时得到的结果..它不会让我运行它..为什么??

请选择一个选项:

  1. 从文件中读取数据。
  2. 将数据写入文件。
  3. 退出。 1 请选择一个选项:

  4. 从文件中读取数据。

  5. 将数据写入文件。

  6. 退出。

  7. 这是我的txt.file loook,如文本文件1234

    sadf
    123
    2
    0
    0
    1
    1
    2
    2
    3
    3
    4
    

1 个答案:

答案 0 :(得分:0)

这是一种逐行读取文件的简单方法:

try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
}

您不需要扫描仪。