我不知道如何从字符串、整数或变量中创建一个特定的数组
Posted
技术标签:
【中文标题】我不知道如何从字符串、整数或变量中创建一个特定的数组【英文标题】:I cannot figure out how to make a specific array from strings, integers, or variables 【发布时间】:2016-11-23 02:31:48 【问题描述】:这些是我的错误:
Exception in thread "main" java.lang.NullPointerException
at Student.<init>(Student.java:22)
at Roster.<init>(Roster.java:35)
at Roster.main(Roster.java:15))
我知道问题是因为我有一个包含 3 个成绩的数组(学生姓名字符串中的最后 3 个)没有正确存储。
import java.util.*;
import java.lang.*;
public class Roster
static String[] students = "1,John,Smith,John1989@gmail.com,20,88,79,59",
"2,Suzan,Erickson,Erickson_1990@gmailcom,19,91,72,85",
"3,Jack,Napoli,The_lawyer99yahoo.com,19,85,84,87",
"4,Erin,Black,Erin.black@comcast.net,22,91,98,82",
"5,Michael,Brauer,mbrauer@wgu.edu,30,99,98,96";
static ArrayList<Student> myStudents = new ArrayList<Student>();
public static void main(String[] args)
new Roster();
public Roster()
for (int i = 0; i < students.length; i++)
String y = students[i];
String[] variable = y.split(",");
String id = variable[0];
String first = variable[1];
String last = variable[2];
String email = variable[3];
int age = Integer.parseInt(variable[4]);
int grade1 = Integer.parseInt(variable[5]);
int grade2 = Integer.parseInt(variable[6]);
int grade3 = Integer.parseInt(variable[7]);
int[] grades = new int[]grade1, grade2, grade3;
//Here is the first issue that I am having: Nothing stores to grades.
Student x = new Student(id, first, last, email, age, grades);
myStudents.add(x);
print_all();
print_invalid_emails();
for (Student y : myStudents)
print_average_grade(y.getId());
public static void print_all()
for (Student x : myStudents)
System.out.println(x);
public static void print_invalid_emails()
for (Student x : myStudents)
String email = x.getEmail();
if (!email.contains("@"))
System.out.println("INVALID EMAIL: " + email);
else if (!email.contains("."))
System.out.println("INVALID EMAIL: " + email);
else if((email.contains(" ")))
System.out.println("INVALID EMAIL: " + email);
remove("3");
remove("3");
public static void remove(String id)
for (int i = 0; i < myStudents.size(); i++)
if (myStudents.get(i).getId().equals(id))
myStudents.remove(i);
System.out.println("STUDENT ID: (" + id + ") *NOT FOUND*");
return;
System.out.println("STUDENT ID: (" + id + ") !DELETED!");
public static void print_average_grade(String id)
for (Student y : myStudents)
if(y.getId().equals(id))
double total = 100;
double average = total / 3;
System.out.println("STUDENT ID: (" + id + ") " + average);
public class Student
String id;
String first;
String last;
String email;
int age;
int[] grades;
public Student(String id, String first, String last, String email, int age, int[] grades)
this.id = id;
this.first = first;
this.last = last;
this.email = email;
this.age = age;
this.grades = grades;
//This is the other problem. I am trying to make an array
public int[] gradeArray = grades[1], grades[2], grades[3];
public String getFirst()
return first;
public void setFirst(String first)
this.first = first;
public String getLast()
return last;
public void setLast(String last)
this.last = last;
public String getEmail()
return email;
public void setEmail(String email)
this.email = email;
public int getAge()
return age;
public void setAge(int age)
this.age=age;
public String getId()
return id;
public void setId(String id)
this.id = id;
public int[] getGradeArray()
return gradeArray;
public void myGrades(int[] grades)
this.grades = grades;
//Print to output
public String toString()
return "STUDENT: ID-" + id + " AGE-" + age + " NAME-" + last + ", " + first;
【问题讨论】:
【参考方案1】:在这里,您将三个等级存储到一个(从 0 开始的)int 数组中:
int[] grades = new int[]grade1, grade2, grade3;
但是您尝试访问超出数组范围的 Grades[3]。
public int[] gradeArray = grades[1], grades[2], grades[3];
另外值得注意的是:Erickson_1990@gmailcom 将无法通过您的有效电子邮件测试。不确定这是出于测试原因,还是只是一个错字。
【讨论】:
以上是关于我不知道如何从字符串、整数或变量中创建一个特定的数组的主要内容,如果未能解决你的问题,请参考以下文章