2011-05-12 32 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());