这个程序应该存储通过键盘输入的输入(名称,id,标记)并将它们存储在一个数组(不是数组列表)中,然后在调用时显示..
但它反过来给了我这个:
Student [studentName=nullnull, studentID=null, project 1 marks =0, ICT marks=0, Active P= 0, Code Assg.=0, ICT2= 0, Active P=0, ICT3 =0, Active P=0 ,]
Student [studentName=nullnull, studentID=null, project 1 marks =0, ICT marks=0, Active P= 0, Code Assg.=0, ICT2= 0, Active P=0, ICT3 =0, Active P=0 ,]
Exception in thread "main" java.lang.NullPointerException
at Main.sortStudentsUsingName(Main.java:164)
at Main.main(Main.java:151)
Student [studentName=nullnull, studentID=null, project 1 marks =0, ICT marks=0, Active P= 0, Code Assg.=0, ICT2= 0, Active P=0, ICT3 =0, Active P=0 ,]
Java Result: 1
这些是我的两个班级(最初不是由我编辑的)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);// for input
System.out.println("Input no. of Student");
int x = sc.nextInt();
Student []array = new Student[x];// create array with size of 5
for (int i = 0; i <x; i++){
System.out.println("Input Student Details "+i);
String fName = sc.next();
String stuID = sc.next();
Student newstudent = new Student();
int projM1;
do {
System.out.println("Please enter the projM1 Marks!");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next();
}
projM1 = sc.nextInt();
} while (projM1 < 0 || projM1 > 100);
int ictM1;
do {
System.out.println("Please enter the ICT1 Marks!");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
ictM1 = sc.nextInt();
} while (ictM1 < 0 || ictM1 > 100);
int actP1;
do {
System.out.println("Please enter the actP1 Marks!");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
actP1 = sc.nextInt();
} while (actP1 < 0 || actP1 > 100);
int codeAsM;
do {
System.out.println("Please enter the codeAsM Marks!");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
codeAsM = sc.nextInt();
} while (codeAsM < 0 || codeAsM > 100);
int ictM2;
do {
System.out.println("Please enter the ictM2 Marks!");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
ictM2 = sc.nextInt();
} while (ictM2 < 0 || ictM2 > 100);
int actP2;
do {
System.out.println("Please enter the actP2 Marks!");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
actP2 = sc.nextInt();
} while (actP2 < 0 || actP2 > 100);
int ictM3;
do {
System.out.println("Please enter the ictM3 Marks!");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
ictM3 = sc.nextInt();
} while (ictM3 < 0 || ictM3 > 100);
int actP3;
do {
System.out.println("Please enter the actP3 Marks!");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
actP3 = sc.nextInt();
} while (actP3 < 0 || actP3 > 100);
/* int component1Marks;
do {
System.out.println("Please enter the component1Marks Marks!");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
component1Marks = sc.nextInt();
} while (component1Marks < 0 || component1Marks > 100);
int component2Marks;
do {
System.out.println("Please enter the component2Marks Marks!");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
component2Marks = sc.nextInt();
} while (component2Marks < 0 || component2Marks > 100);
int component3Marks;
do {
System.out.println("Please enter the component3Marks Marks!");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
component3Marks = sc.nextInt();
} while (component3Marks < 0 || component3Marks > 100); */
array[i] = new Student();
}
for(Student newstudent : array){
System.out.println(newstudent);
}
sortStudentsUsingName(array); // sort method is called
for(Student newstudent : array){
System.out.println(newstudent);
}
}
public static void sortStudentsUsingName(Student []array){
for (int i = 0; i < array.length; i++){
Student min = array[i];
int minIndex = i;
for (int j = i + 1; j < array.length; j++){
if (min.getfName().compareTo(array[j].getfName()) > 0){
// How compareTo method works
//a.compareTo(b) == 0 means both a and b are equal
//a.compareTo(b) > 0 means a is large and b is small
//a.compareTo(b) < 0 means b is large and a is small
//for Descending order change the conditions to:
//min.getStudentName().compareTo(array[j].getStudentName()) < 0
min = array[j];
minIndex = j;
}
}
if (i != minIndex){
Student temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
}
}
}
这是学生班
/**
* @author Guhanathan Poravi
*
*/
public class Student {
private String fName;
private String lName;
private String stuID;
private int projM1;
private int ictM1;
private int actP1;
private int codeAsM;
private int ictM2;
private int actP2;
private int ictM3;
private int actP3;
private double component1Marks = (projM1 * 0.3 + ictM1 * 0.6 + actP1 * 0.1) ;
private double component2Marks = (codeAsM * 0.5 + ictM2 * 0.4 + actP2 * 0.1);
private double component3Marks = (ictM3 *0.9 + actP3 * 0.1);
private double moduleMarks =(component1Marks + component2Marks + component3Marks);
private String fGrade;
public Student(/*String fName, String stuID, int projM1, int ictM1,
int actP1, int codeAsM, int ictM2,
int actP2, int ictM3, int actP3,double component1Marks,double component2Marks, double component3Marks, double moduleMarks */) {
super();
this.fName = fName;
this.lName = lName;
this.stuID = stuID;
this.projM1 = projM1;
this.ictM1 = ictM1;
this.actP1 = actP1;
this.codeAsM = codeAsM;
this.ictM2 = ictM2;
this.actP2 = actP2;
this.ictM3 = ictM3;
this.actP3 = actP3;
this.component1Marks = (projM1 * 0.3 + ictM1 * 0.6 + actP1 * 0.1) ;
this.component2Marks = (codeAsM * 0.5 + ictM2 * 0.4 + actP2 * 0.1);
this.component3Marks = (ictM3 *0.9 + actP3 * 0.1);
this.moduleMarks = (component1Marks + component2Marks + component3Marks);
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public String getStuID() {
return stuID;
}
public void setStuID(String stuID) {
this.stuID = stuID;
}
public int getProjM1() {
return projM1;
}
public void setProjM1(int projM1) {
this.projM1 = projM1;
}
public int getIctM1() {
return ictM1;
}
public void setIctM1(int ictM1) {
this.ictM1 = ictM1;
}
public int getActP1() {
return actP1;
}
public void setActP1(int actP1) {
this.actP1 = actP1;
}
public int getCodeAsM() {
return codeAsM;
}
public void setCodeAsM(int codeAsM) {
this.codeAsM = codeAsM;
}
public int getIctM2() {
return ictM2;
}
public void setIctM2(int ictM2) {
this.ictM2 = ictM2;
}
public int getActP2() {
return actP2;
}
public void setActP2(int actP2) {
this.actP2 = actP2;
}
public int getIctM3() {
return ictM3;
}
public void setIctM3(int ictM3) {
this.ictM3 = ictM3;
}
public int getActP3() {
return actP3;
}
public void setActP3(int actP3) {
this.actP3 = actP3;
}
public double getComponent1Marks() {
return component1Marks;
}
public void setComponent1Marks(int component1Marks) {
this.component1Marks = component1Marks;
}
public double getComponent2Marks() {
return component2Marks;
}
public void setComponent2Marks(int component2Marks) {
this.component2Marks = component2Marks;
}
public double getComponent3Marks() {
return component3Marks;
}
public void setComponent3Marks(int component3Marks) {
this.component3Marks = component3Marks;
}
public double getModuleMarks() {
return moduleMarks;
}
public void setModuleMarks(double moduleMarks) {
this.moduleMarks = moduleMarks;
}
@Override
public String toString() {
return "Student [studentName=" + fName+lName + ", studentID="
+ stuID + ", project 1 marks =" + projM1 + ", ICT marks=" + ictM1 +
", Active P= " + actP1 + ", Code Assg.=" + codeAsM +","
+ " ICT2= " + ictM2 +", Active P=" + actP2 +", ICT3 =" + ictM3
+ ", Active P=" + actP3 +" ,]";
}
}
我该如何纠正?我的输入是否进入阵列?如果是这样,为什么它给我这个输出?
答案 0 :(得分:0)
您使用空Student
实例填充数组(使用无参数构造函数创建,以后不会设置它们的属性)。这就是他们打印所有属性的默认值(0和空值)的原因。
发生NullPointerException
是因为您尝试按学生名称对数组进行排序,而所有名称都为空。
此代码似乎与您的previous question相关,您可以在创建每个Student实例后设置Student属性。如果希望它们具有值,则应设置属性。
答案 1 :(得分:0)
您没有将fName
和stuID
的值设置为newstudent
这一行之后:
Student newstudent = new Student();
你应该这样做:
newstudent.setFName(fname);
newstudent.setStuID(stuID);
然后在您的评论代码之后:
array[i] = new Student();
应该是:
array[i] = newstudent;