我在这里引用了一些修正,但仍然完全满足我的程序,但仍然没有完成 问题1 =我的displayReg()出来" nullnullnullnull ..." ,我应该使用arraylist吗? 问题2 =我需要选择通过IC和NAME搜索searchReg()我该怎么做? 问题3 =需要在.txt文件上显示displayReg。
import java.util.*;
public class RegisterMenu {
private Driver[] newOwner;
private final int MAX_ITEMS = 10;
private int size = 0;
public Driver newReg(){
Driver owner = new Driver();
Scanner scan = new Scanner(System.in);
owner.setRegNo(size+1);
System.out.print("Enter Name: ");
owner.setName(scan.nextLine());
System.out.print("Enter IC: ");
owner.setIc(scan.nextLine());
System.out.print("Enter PlateNo: ");
owner.carInfo.setPlateNum(scan.nextLine());
System.out.print("Enter Color: ");
owner.carInfo.setColor(scan.nextLine());
System.out.print("Enter Year: ");
owner.carInfo.setYear(scan.nextLine());
System.out.print("Enter Make: ");
owner.carInfo.setMake(scan.nextLine());
System.out.print("Enter Model: ");
owner.carInfo.setModel(scan.nextLine());
System.out.print("Enter Capacity: ");
owner.carInfo.setCapacity(scan.nextLine());
return owner;
}
public Driver editReg(){
Scanner scan = new Scanner(System.in);
System.out.print("Enter RegNo to be edit: ");
int input = scan.nextInt();
Driver owner = newReg();
newOwner[input] = owner;
return owner;
}
public Driver searchReg(){
}
public void displayReg(){
for(int i = 0; i < newOwner.length; i++){
newOwner[i].toString();
}
}
public RegisterMenu(){
newOwner = new Driver[MAX_ITEMS];
Scanner scan = new Scanner(System.in);
System.out.println("1. Register New Car");
System.out.println("2. Edit Car Information");
System.out.println("3. Search Car Information");
System.out.println("4. Display Car List");
System.out.println("5. Exit");
System.out.print("Enter Selection: ");
int s = scan.nextInt();
switch(s){
case 1:
System.out.println("--Register New Car--");
if (size < MAX_ITEMS) {
Driver owner = newReg();
newOwner[size++] = owner;
}
break;
case 2:
System.out.println("--Edit Car Infomation--");
editReg();
break;
case 3:
System.out.println("--Search Car Infomation--");
break;
case 4:
System.out.println("--Display Car Infomation--");
displayReg();
break;
case 5:
System.exit(0);
default:
System.out.println("Error selection");
}
}
public static void main (String args[]){
while(true){
RegisterMenu owner = new RegisterMenu();
}
}
}
这是我的Car class
public class Car {
public String plateNum;
public String make;
public String model;
public String color;
public String year;
public String capacity;
public Car(){
}
public Car(String plateNum, String color, String year, String make, String model, String capacity){
this.plateNum = plateNum;
this.color = color;
this.year = year;
this.make = make;
this.model = model;
this.capacity = capacity;
}
public String getPlateNum(){
return plateNum;
}
public String getMake(){
return make;
}
public String getModel(){
return model;
}
public String getColor(){
return color;
}
public String getYear(){
return year;
}
public String getCapacity(){
return capacity;
}
public void setPlateNum(String plateNum){
this.plateNum = plateNum;
}
public void setMake(String make){
this.make = make;
}
public void setModel(String model){
this.model = model;
}
public void setColor(String color){
this.color = color;
}
public void setYear(String year){
this.year = year;
}
public void setCapacity(String capacity){
this.capacity = capacity;
}
}
这是我的Driver类
public class Driver {
private int regNo;
private String name;
private String ic;
Car carInfo = new Car();
public Driver(){
}
public Driver(int regNo, String name, String ic, Car carInfo){
this.regNo = regNo;
this.name = name;
this.ic = ic;
this.carInfo = carInfo;
}
public int getRegNo(){
return regNo;
}
public String getName(){
return name;
}
public String getIc(){
return ic;
}
public void setRegNo(int regNo){
this.regNo = regNo;
}
public void setName(String name){
this.name = name;
}
public void setIc(String ic){
this.ic = ic;
}
public String toString(){
return "RegNo: "+getRegNo()+"\tName: "+getName()+"\tIc: "+getIc()+
"\tPlateNo: "+carInfo.getPlateNum()+"\tColor: "+carInfo.getColor()+"\tYear: "+carInfo.getYear()+
"\tMake: "+carInfo.getMake()+"\tModel: "+carInfo.getModel()+"\tCapacity: "+carInfo.getCapacity();
}
}
答案 0 :(得分:1)
答案 1 :(得分:1)
int i = 0;
Register[] a = new Register[i];
您正在尝试创建0大小的数组。但是当你尝试在i = 0时访问[i]时,它会尝试访问数组中第1个位置的元素(数组的第一个位置为0)。
此外,您已将其置于while循环中,这意味着您在每个循环周期中创建一个0大小的新数组。
尝试将i = 10(或您的程序逻辑所说的任何内容)并将数组创建放在循环中