开发者

print only array items that are greater than a certain int

I wrote this simple script that reads a text file and then adds the data to an array however i only want to print the first and last name of the students that have over 90 credits

Student[] student = new Student[50];

        Scanner userin, filein;
        String filename;
        int numStudents;

        // Get the filename from the user, with exce开发者_JAVA技巧ption 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)); 


"script"?

This is some ugly code. Formatting and structure matter. I'd recommend paying more attention to it in the future. It'll make your maintenance life easier as your programs (aka "scripts") become more complex.

Learn the Sun Java coding standards. You aren't following them now.

Everything in Java has to be a part of a class. I'll assume that you know that. This must be a snippet you cut out of your class.

Your code is too chaotic to worry about making it nice. This'll work:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜