2017-04-30 94 views
1

我工作的學校最後的項目,有一些麻煩。我從來沒有在Java中進行任何編程,也沒有任何面向對象的編程,所以我的代碼可能不會那麼好。ArrayList的錯誤 - 無法找到象徵

我有我的數組列表的一些問題。我遇到一個錯誤,說找不到符號 - class studentInfo。我在做什麼我的代碼不正確?這阻止了我編譯代碼。所有的學生信息都是假的。

而且會有人有關於如何清理或優化我的代碼的任何建議?

import java.util.ArrayList; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class Roster 
{ 
    private static ArrayList<studentInfo> studentList = new ArrayList<>(); 

    public static void main(String[] args) 
    { 

     add(1, "John", "Smith", "[email protected]", 20, 88, 79, 59); 
     add(2, "Suzan", "Erickson", "[email protected]", 19, 91, 72, 85); 
     add(3, "Jack", "Napoli", "The_lawyer99yahoo.com", 19, 85, 84, 87); 
     add(4, "Erin", "Black", "[email protected]", 22, 91, 98, 82); 
     print_all(); 
     print_invalid_emails(); 
     print_average_grade(2); 
     remove(3); 
     remove(3); 



    } 
    public static void add (int studentID, String firstName, String lastName, String email, int age, int grade1, int grade2, int grade3) 
    { 
     int[] grades = {grade1, grade2, grade3}; 
     studentInfo newStudent = new studentInfo (studentID, firstName, lastName, email, age, grades); 
     studentList.add(newStudent); 
    } 

    public static void remove(int studentID) 
    { 
     for (studentInfo i: studentList) { 
      if (i.getStudentID == studentID) { 
       studentList.remove(i); 
       System.out.println("Student Removed"); 
      } 
      else { 
      System.out.println("A student with this ID(studentID) was not found."); 
      return; 
      } 

     } 
    } 
    public static void print_all(){ 
    for (studentInfo i: studentList) { 
     studentList.get(i).print(); 
    } 
    } 
    public static void print_average_grade(int studentID) { 
     studentList.get(studentID).getAverageGrade(); 
} 

public static void print_invalid_emails() { 
    String RFC5322 = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-][email protected][a-zA-Z0-9.-]+$"; 
    Pattern pattern = Pattern.compile(RFC5322); 
    for (studentInfo i: studentList) { 
     String email = i.getEmail; 
     Matcher matcher = pattern.matcher(email); 
     System.out.println(email +":\t"+ matcher.matches()); 
    } 

} 


} 

我的第二類

public class Student 
{ 
    private int studentID; 
    private String firstName; 
    private String lastName; 
    private String email; 
    private int age; 
    private int[] grades; 

    public Student (int studentID, String firstName, String lastName, String email, int age, int grades[]) 
    { 
    setStudentID(studentID); 
    setFirstName(firstName); 
    setLastName(lastName); 
    setEmail(email); 
    setAge(age); 
    setGrades(grades); 

    } 

    public void setStudentID(int studentID) { 
     this.studentID = studentID; 
    } 

    public void setFirstName(String firstName) 
    { 
     this.firstName = firstName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 

    public void setGrades(int[] grades) { 
     this.grades = grades; 
    } 

    public int getStudentID() { 
     return studentID; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public String getFullName() { 
     return firstName + lastName; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public int getAge() { 
     return age; 
    } 

    public int[] getGrades() { 
     return grades; 
    } 

    public int getAverageGrade() { 
     int averageGrade = 0; 
      for (int grade : grades) { 
       averageGrade += grade; 
      } 
     return averageGrade; 
    } 
    public void print() { 
     System.out.println(
     "Student Name:\t" + getFullName() + "\n " + 
     "Student ID:\t" + getStudentID() + "\n" + 
     "Email:\t" + getEmail() + "\n" + 
     "Age:\t" + getAge() + "\n" + 
     "Average Grade:\t" + getAverageGrade() 
     ); 

    } 


} 
+1

你的意思'Student',而不是'studentInfo'? –

回答

1

我做了如下修改代碼:

  • ArrrayList應存儲Student類型的對象。 studentInfo不引用任何定義的類。
  • 在方法add,更改的newStudent類型Student
  • 請在removeprint_allprint_invalid_emails方法for循環同樣必要的調整。

改正的代碼如下所示:

import java.util.ArrayList; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class Roster 
{ 
    private static ArrayList<Student> studentList = new ArrayList<>(); 

    public static void main(String[] args) 
    { 

     add(1, "John", "Smith", "[email protected]", 20, 88, 79, 59); 
     add(2, "Suzan", "Erickson", "[email protected]", 19, 91, 72, 85); 
     add(3, "Jack", "Napoli", "The_lawyer99yahoo.com", 19, 85, 84, 87); 
     add(4, "Erin", "Black", "[email protected]", 22, 91, 98, 82); 
     print_all(); 
     print_invalid_emails(); 
     print_average_grade(2); 
     remove(3); 
     remove(3);  
    } 

    public static void add (int studentID, String firstName, String lastName, String email, int age, int grade1, int grade2, int grade3) 
    { 
     int[] grades = {grade1, grade2, grade3}; 
     Student newStudent = new Student (studentID, firstName, lastName, email, age, grades); 
     studentList.add(newStudent); 
    } 

    public static void remove(int studentID) 
    { 
     for (Student i: studentList) { 
      if (i.getStudentID() == studentID) { 
       studentList.remove(i); 
       System.out.println("Student Removed"); 
      } 
      else { 
      System.out.println("A student with this ID(studentID) was not found."); 
      return; 
      } 

     } 
    } 
    public static void print_all(){ 
    for (Student i: studentList) { 
     i.print(); 
    } 
    } 
    public static void print_average_grade(int studentID) { 
     studentList.get(studentID).getAverageGrade(); 
} 

public static void print_invalid_emails() { 
    String RFC5322 = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-][email protected][a-zA-Z0-9.-]+$"; 
    Pattern pattern = Pattern.compile(RFC5322); 
    for (Student i: studentList) { 
     String email = i.getEmail(); 
     Matcher matcher = pattern.matcher(email); 
     System.out.println(email +":\t"+ matcher.matches()); 
    } 

} 


} 
+0

這就是它!我發現在將它更改爲Student而不是StudentInfo時,我不斷收到一些錯誤,因爲我正在使用例如i.getEmail,它將該方法引用爲變量而不是i.getEmail()。 – gmart

+0

很高興爲你效勞。不要忘記給予好評太:d Exaxtly左右。省略方法調用中的括號是一個非常常見的編碼錯誤 –