1.Java to create a custom class array method:
Student []stu = new student[3];
for (int i = 0; i < 3; i + +)
{
Stu[i] = new Student ();
}
2. Otherwise you will be prompted null pointer exception
Package project;
Import java.io.*;
Import Java.util.Scanner;
Class Student {private int id;
private String name;
private int score;
public void setId (int id) {this.id = ID;
public int getId () {return this.id;
public void SetName (String name) {this.name = name;
Public String GetName () {return this.name;
The public void SetScore (int score) {This.score = score;
public int Getscore () {return this.score;
} public class Project2 {File File = new file ("E:/data.txt");
FileWriter filewrite = null;
BufferedWriter write = null;
FileReader fileread = null;
BufferedReader read = null;
Student []stu = new student[3];
public void put () {try {filewrite = new FileWriter (file);
catch (IOException e) {//TODO automatically generated catch block E.printstacktrace ();
Write = new BufferedWriter (filewrite); for (int i = 0; i < 3; i + +) {System.out.println ("Please enter the first" + (i +)1 + "A student's ID, name, Grade:");
Scanner in = new Scanner (system.in);
try {String str = in.nextline ();
String data[] = Str.split ("");
for (int j = 0; J < 3; J + +) {Write.write (data[j]);
Write.newline ();
The Catch Block E.printstacktrace () is automatically generated by catch (IOException e) {//TODO);
} try {write.close ();
Filewrite.close ();
catch (IOException e) {//TODO automatically generated catch block E.printstacktrace ();
The public void get () {int sum = 0;
Double Ave;
try {fileread = new FileReader (file);
catch (FileNotFoundException e) {//TODO automatically generated catch block E.printstacktrace ();
Read = new BufferedReader (fileread);
for (int i = 0; i < 3; i + +) {Stu[i] = new Student ();
try {stu[i].setid (Integer.parseint (Read.readline ()));
Stu[i].setname (Read.readline ()); Stu[i].setScore (Integer.parseint (Read.readline ()));
catch (Exception e) {//TODO automatically generated catch block E.printstacktrace ();
for (int i = 0; i < 3; i + +) {sum + = Stu[i].getscore ();
Ave = SUM * 1.0/3;
SYSTEM.OUT.PRINTLN ("Student's average score is:" + ave);
try {read.close ();
Fileread.close ();
catch (IOException e) {//TODO automatically generated catch block E.printstacktrace ();
} public static void Main (String []args) {Project2 Pro = new Project2 ();
Pro.put ();
Pro.get (); }
}
Summarize:
This allows us to define the desired array within the project, depending on the requirements of the project.