2011-05-12 38 views
0

我編寫了這個簡單的腳本來讀取文本文件,然後將數據添加到數組中但我只想打印超過90的學生的名字和姓氏學分僅打印大於特定int的數組項目

Student[] student = new Student[50]; 

     Scanner userin, filein; 
     String filename; 
     int numStudents; 

     // Get the filename from the user, with exception handling to deal with incorrect file names 
     userin = new Scanner(System.in); 
     filename = null; 
     filein = null; 
      filename = Students.txt; 
      try { 
       filein = new Scanner(new FileReader(filename)); // try to open the file 
      } catch (FileNotFoundException e) { // failed to open the file 
       System.out.println("Invalid file - try again"); 
       filename = null; 
      } 

numStudents = 0; 
     while (filein.hasNext()){ 
String lastName = filein.next(); 
String firstName = filein.next();    
double gpa = filein.next(); 
int Credits = filein.next(); 

      student[numStudents++] = new Student(lastName,firstName,gpa,Credits);  

     }  

int i=0; 
     do 
     { 


      System.out.println(student[i].toString()); 

      i++; 


     } 
    while ((student[i] != null)&&(i <= student.length)); 

回答

2

「script」?

這是一些的代碼。格式和結構問題。我建議將來更多地關注它。隨着程序(又名「腳本」)變得更加複雜,它將使您的維護生活變得更加輕鬆。

瞭解Sun Java coding standards。你現在沒有關注他們。

Java中的所有東西都必須是類的一部分。我會假設你知道這一點。這必須是你從班上刪去的片段。

你的代碼太混亂了,不用擔心它會變得很好。這將工作:

if (student[i].getCredits() >= 90) 
     System.out.println(student[i].toString());