问题
after creating an array how can display all the details in that array? here is what i did so far..
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Student s = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of Student");
int x = sc.nextInt();
Student [] studentDetails = new Student[x];
for(int i = 0; i < x; i++) {
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter f name");
studentDetails[i].setfName(sc.nextLine());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter l name");
studentDetails[i].setlName(sc.nextLine());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter Reg no");
studentDetails[i].setStuID(sc.nextLine());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter Prj 1 marks");
studentDetails[i].setProjM1(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter ICT 1 Marks ");
studentDetails[i].setIctM1(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter Active P 1");
studentDetails[i].setActP1(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter Code Assg");
studentDetails[i].setCodeAsM(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter ICT 2 Marks");
studentDetails[i].setIctM2(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter Active P Marks");
studentDetails[i].setActP2(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter ICT 3 Marks");
studentDetails[i].setIctM3(sc.nextInt());
studentDetails[i] = new Student("", "", "", 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter Active P 3");
studentDetails[i].setActP3(sc.nextInt());
System.out.println(Student.toString(studentDetails));
}
}
}
this is the Student Class,
package cw;
public class Student {
private static String fName;
private static String lName;
private static String stuID;
private static int projM1;
private static int ictM1;
private static int actP1;
private static int codeAsM;
private static int ictM2;
private static int actP2;
private static int ictM3;
private static int actP3;
private static int component1Marks ;
private static int component2Marks;
private static int component3Marks;
private static double moduleMarks;
public Student(String fName, String lName, String stuID, int projM1, int ictM1, int actP1,
int codeAsM,int ictM2, int actP2, int ictM3, int actP3, int component1Marks,
int component2Marks, int component3Marks, int 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;
}
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 int getComponent1Marks() {
return component1Marks;
}
public void setComponent1Marks(int component1Marks) {
this.component1Marks = component1Marks;
}
public int getComponent2Marks() {
return component2Marks;
}
public void setComponent2Marks(int component2Marks) {
this.component2Marks = component2Marks;
}
public int getComponent3Marks() {
return component3Marks;
}
public void setComponent3Marks(int component3Marks) {
this.component3Marks = component3Marks;
}
public double getModuleMarks() {
return moduleMarks;
}
public void setModuleMarks(double moduleMarks) {
this.moduleMarks = moduleMarks;
}
public static String toString(Student[] studentDetails) {
return "Student [fName=" + fName +", Lname=" + lName + ", studentID="
+ stuID + ", marksProject1=" + projM1 + ", marksICT1="
+ ictM1 + ", ActiveP=" + actP1 + ", Code Ass="
+ codeAsM + ", ICT2=" + ictM2 + ", ActiveP2=" + actP2 + ", ICT3=" + ictM3 + ", ActiveP3="
+ actP3 + "]";
}
}
when program is executed it asks for inputs. but when i input values,this is the output i get,,
Student [fName=, Lname=, studentID=, marksProject1=0, marksICT1=0, ActiveP=0, Code Ass=0, ICT2=0, ActiveP2=0, ICT3=0, ActiveP3=76]
Note that only ActiveP3 value is correctly printed!!
how to correct this? i am new to java.this is my first assignment.
回答1:
Create only one new object in each iteration and use an empty constructor as Eran suggested:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of Student");
int numberOfStudents = sc.nextInt();
Student[] studentDetails = new Student[numberOfStudents];
for (int i = 0; i < numberOfStudents; i++) {
Student newStudent = new Student();
System.out.println("enter f name");
newStudent.setfName(sc.next());
System.out.println("enter l name");
newStudent.setlName(sc.next());
System.out.println("enter Reg no");
newStudent.setStuID(sc.next());
System.out.println("enter Prj 1 marks");
newStudent.setProjM1(sc.nextInt());
System.out.println("enter ICT 1 Marks ");
newStudent.setIctM1(sc.nextInt());
System.out.println("enter Active P 1");
newStudent.setActP1(sc.nextInt());
System.out.println("enter Code Assg");
newStudent.setCodeAsM(sc.nextInt());
System.out.println("enter ICT 2 Marks");
newStudent.setIctM2(sc.nextInt());
System.out.println("enter Active P Marks");
newStudent.setActP2(sc.nextInt());
System.out.println("enter ICT 3 Marks");
newStudent.setIctM3(sc.nextInt());
System.out.println("enter Active P 3");
newStudent.setActP3(sc.nextInt());
studentDetails[i] = newStudent;
System.out.println(newStudent);
}
sc.close();
}
}
Add the empty constructor to your Student
class, don't make the variables static
and modify the toString()
method:
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 int component1Marks;
private int component2Marks;
private int component3Marks;
private double moduleMarks;
public Student() {
}
.
.
.
@Override
public String toString() {
return "Student [fName=" + fName + ", Lname=" + lName + ", studentID=" + stuID + ", marksProject1=" + projM1
+ ", marksICT1=" + ictM1 + ", ActiveP=" + actP1 + ", Code Ass=" + codeAsM + ", ICT2=" + ictM2
+ ", ActiveP2=" + actP2 + ", ICT3=" + ictM3 + ", ActiveP3=" + actP3 + "]";
}
}
回答2:
You are only supposed to call the Student constructor once for each entry in the array :
studentDetails[i] = new Student("", "", "", 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0);
Calling it multiple times for the same value of i
overwrites the previous reference stored in the i
th position of the array.
And it's pointless to call a constructor with so many parameters only to pass default values for all of them. Use a parameter-less constructor.
In addition, your toString()
method of the Student
class doesn't need any parameters, since it just prints the properties of the single Student
it is called for.
You should change the call :
System.out.println(Student.toString(studentDetails));
to
System.out.println(studentDetails[i].toString());
Your loop should look like this :
for(int i = 0; i < x; i++) {
// this can be replaced by studentDetails[i] = new Student();
studentDetails[i] =
new Student("", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter f name");
studentDetails[i].setfName(sc.nextLine());
System.out.println("enter l name");
studentDetails[i].setlName(sc.nextLine());
System.out.println("enter Reg no");
studentDetails[i].setStuID(sc.nextLine());
System.out.println("enter Prj 1 marks");
studentDetails[i].setProjM1(sc.nextInt());
System.out.println("enter ICT 1 Marks ");
studentDetails[i].setIctM1(sc.nextInt());
System.out.println("enter Active P 1");
studentDetails[i].setActP1(sc.nextInt());
System.out.println("enter Code Assg");
studentDetails[i].setCodeAsM(sc.nextInt());
System.out.println("enter ICT 2 Marks");
studentDetails[i].setIctM2(sc.nextInt());
System.out.println("enter Active P Marks");
studentDetails[i].setActP2(sc.nextInt());
System.out.println("enter ICT 3 Marks");
studentDetails[i].setIctM3(sc.nextInt());
System.out.println("enter Active P 3");
studentDetails[i].setActP3(sc.nextInt());
System.out.println(studentDetails[i]);
}
And change your toString
method to :
public String toString() {...}
回答3:
public class Main {
public static void main(String[] args){
Student student = null;
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of Student");
int x = sc.nextInt();
Student [] studentDetails = new Student[x];
for(int i = 0; i < x; i++) {
student = new Student("", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
System.out.println("enter f name");
student.setfName(sc.nextLine());
System.out.println("enter l name");
student.setlName(sc.nextLine());
System.out.println("enter Reg no");
student.setStuID(sc.nextLine());
System.out.println("enter Prj 1 marks");
student.setProjM1(sc.nextInt());
System.out.println("enter ICT 1 Marks ");
student.setIctM1(sc.nextInt());
System.out.println("enter Active P 1");
student.setActP1(sc.nextInt());
System.out.println("enter Code Assg");
student.setCodeAsM(sc.nextInt());
System.out.println("enter ICT 2 Marks");
student.setIctM1(sc.nextInt());
System.out.println("enter Active P Marks");
student.setActP1(sc.nextInt());
System.out.println("enter ICT 3 Marks");
student.setIctM3(sc.nextInt());
System.out.println("enter Active P 3");
student.setActP1(sc.nextInt());
student = new Student(student.getfName(), student.getlName(), student.getStuID(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
studentDetails[i] = student;
System.out.println(Student.toString(studentDetails));
}
}
}
As you are creating array of student objects set the value student object and add object to student array
来源:https://stackoverflow.com/questions/26773663/how-to-display-stored-information-in-an-array-in-java