ArrayList错误 - 找不到符号

时间:2017-04-30 20:46:28

标签: java arraylist cannot-find-symbol

我正在为学校做最后的项目并遇到一些麻烦。我从未在Java中进行任何编程,或者实际上没有任何面向对象的编程,所以我的代码可能不会那么好。

我的数组列表存在一些问题。我收到一个错误,说无法找到符号 - 类studentInfo。我的代码错误地做了什么?这使我无法编译代码。所有学生信息都是假的。

也有人会就如何清理或优化我的代码提出任何建议吗?

<div id="one">one</div>
<div id="two">two</div>
<div id="three">three</div>
<div id="four">four</div>
</br>
<div id="five">five</div>
<div id="six">Six</div>
<div id="seven">Seven</div>
<div id="eight">Content for id "eight" Goes Here</div>

我的第二堂课

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", "John1989@gmail.com", 20, 88, 79, 59);
        add(2, "Suzan", "Erickson", "Erickson_1990@gmailcom", 19, 91, 72, 85);
        add(3, "Jack", "Napoli", "The_lawyer99yahoo.com", 19, 85, 84, 87);
        add(4, "Erin", "Black", "Erin.black@comcast.net", 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_!#$%&'*+/=?`{|}~^.-]+@[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());
     }

 }


}

1 个答案:

答案 0 :(得分: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", "John1989@gmail.com", 20, 88, 79, 59);
        add(2, "Suzan", "Erickson", "Erickson_1990@gmailcom", 19, 91, 72, 85);
        add(3, "Jack", "Napoli", "The_lawyer99yahoo.com", 19, 85, 84, 87);
        add(4, "Erin", "Black", "Erin.black@comcast.net", 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_!#$%&'*+/=?`{|}~^.-]+@[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());
     }

 }


}