2016-08-12 1 views
0

それぞれの行は、名前(String型)、ID(String型)、およびgpa(double型)の空白で区切られた3つの情報で構成されています。学生クラスの作成

私は、整数Nとファイル名を読み込み、その後、入力ファイルからのデータのNラインを読み、学生のArrayListの中にデータを格納するプログラムを書くことをしようとしています。この質問では、Studentクラスが与えられていると仮定できます。私はコードに問題があり、13のエラーのようになっています。もし誰かが驚くべきことを助けることができれば!

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.InputMismatchException; 
import java.util.Scanner; 
import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 

public class Students { 

    public static void main(String[] args) { 
     ArrayList<Student> students = new ArrayList<Student>(); //this will be a list of all the students in the file 
     //we will add to this list as we read in students from the file 
     //using an ArrayList allows us to easily add students when we don't know how many there will be 
     Scanner stu = new Scanner(System.in); 
     Student s = null; 

     /*Part two: get the file name and intialize the file reader*/ 
     try{ 
      System.out.print("Enter filename: "); //prompt the user for the file name 
      filename = in.readLine(); //get the filename- user types this on the keyboard 

      fin = new BufferedReader(new FileReader(filename)); //create the file reader 
      //if the filename is invalid, an error message will be printed and the program terminated 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

     /*Part three: read all of the student data from the file*/ 
     s = getStudent(fin); //call the getStudent function to get the next student from the file 
     while(s != null){ //keep going until all students have been read 
      students.add(s); //add the new student to our ArrayList of students 
      s = getStudent(fin); //get the next student 
     } 


     /*Part four: print out the results*/ 
     for(Student a: students){ //loop through all the students in our list 
      System.out.println(a.getFirstName()); //print out name 
      System.out.println(a.getId()); //print out ID number 
      System.out.println("Gpa: " + a.getGrade()); //print out gpa 



    } 
} 
} 

Test case 

Enter an integer: 3 
Enter a filename: students.txt 
[Wally 1234567 4.5, John 7654321 3.0, Susan 1212121 4.5] 
+0

あなたは、私が "中" を宣言する必要がない理由 "フィン" – ravthiru

+0

、 "中" の変数を宣言する必要がありますか?スキャナの一部ではありません –

+0

[スキャナ](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html)クラスにはreadLine()メソッドがありません。おそらく[BufferedReader](https://docs.oracle.com/javase/7/docs/api/java/io/BufferedRead er.html#readLine())を参照してください。あなたがJVMに 'in'を宣言する必要があります。 – davedwards

答えて

1

これは私が思い付いたし、それをローカルに実行するものであり、それは動作します。..

package tes; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.util.ArrayList; 
import java.util.Scanner; 

class Student { 

    private String name; 
    private String ID; 
    private Double gpa; 
    public Double getGpa() { 
     return gpa; 
    } 
    @Override 
    public String toString() { 
     return "Student [name=" + name + ", ID=" + ID + ", gpa=" + gpa + "]"; 
    } 
    public void setGpa(Double gpa) { 
     this.gpa = gpa; 
    } 
    public String getID() { 
     return ID; 
    } 
    public void setID(String ID) { 
     this.ID = ID; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
} 
public class Students { 


    public static void main(String[] args) { 

     try{ 


      ArrayList<Student> students = new ArrayList<Student>(); 
      Scanner scanner = new Scanner(System.in); 
      System.out.print("Enter filename: "); //prompt the user for the file name 
      String fileName = scanner.next(); 
      File file = new File(fileName); 

      if (!file.exists()) { 
       throw new FileNotFoundException("file not exits"); 
      } 

      BufferedReader reader = new BufferedReader(new FileReader(file)); 
      String currentline = ""; 
      while ((currentline = reader.readLine()) != null) { 

       String[] linearray = currentline.split(","); 
       for (int i=0;i<linearray.length;i++) { 
         String record = linearray[i]; 
         String[] r1 = record.split(" "); 
         Student student = new Student(); 
         student.setName(r1[0]); 
         student.setID(r1[1]); 
         student.setGpa(Double.parseDouble(r1[2])); 
         students.add(student); 
       } 

      }  

      System.out.println(students); 



     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

     } 
} 

データを入力ファイル:

  • ウォーリー1234567 4.5、ジョン7654321 3.0、スーザン1212121 4.5
  • サリー934567 3.75、ブラウン7654321 4.0、リリー2212121 4.5

出力:

Enter filename: C:\Users\yc03ak1\Desktop\testing.txt 
[Student [name=Wally, ID=1234567, gpa=4.5], Student [name=John, ID=7654321, gpa=3.0], Student [name=Susan, ID=1212121, gpa=4.5], Student [name=Sally, ID=934567, gpa=3.75], Student [name=Brown, ID=7654321, gpa=4.0], Student [name=Lilly, ID=2212121, gpa=4.5]] 

HTH ..

+0

何らかの理由でエラーが発生しました –

+0

Students.java:38:シンボルが見つかりません シンボル:メソッドsetID(java.lang.String) 場所:クラスtes.Student student.setID(r1 [1]); –

+0

IDフィールドをチェックすると、両方のクラスで同じでなければならず、両方のクラスが同じパッケージに入っているかどうかを確認できますか? – user641887

関連する問題