在数组中输入数据并显示 null 时出现程序错误

2022-01-19 00:00:00 arrays switch-statement java

我通过参考这里做了一些更正,但仍然对我的程序非常满意,它仍然不完整.
问题 1 = 我的 displayReg() 出现 "nullnullnullnull..." ,我应该使用 arraylist 吗?
问题 2 = 我需要在 searchReg() 中选择按 IC 和 NAME 进行搜索,我该怎么做?
问题 3 = 需要在 .txt 文件上显示 displayReg.

i have done some correction by referring in here, but still haven quite satisfy of my program and it still not complete.
Problem 1 = my displayReg() comes out "nullnullnullnull..." , should i use arraylist?
Problem 2 = i need to have option to search by IC and NAME in searchReg() how can i do that?
Problem 3 = need to display displayReg on .txt file.

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();

        }
    }


}

这是我的汽车课

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;
    }




}

这是我的驱动程序类

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()+"	Name: "+getName()+"	Ic: "+getIc()+
               "	PlateNo: "+carInfo.getPlateNum()+"	Color: "+carInfo.getColor()+"	Year: "+carInfo.getYear()+
                       "	Make: "+carInfo.getMake()+"	Model: "+carInfo.getModel()+"	Capacity: "+carInfo.getCapacity();
   }

}

推荐答案

int i = 0;
Register[] a = new Register[i];   

您实际上是在尝试创建一个大小为 0 的数组.但是当您尝试在 i = 0 时访问 a[i] 时,它会尝试访问数组中第一个位置的元素(数组的第一个位置为 0).

You are effectively attempting to create an array of 0 size. But when you try to access a[i] when i = 0, it attempts to access the element at 1'st position in the array (array have 0 as the first position).

你还把它放在了一个while循环中,这意味着你在每个循环周期创建一个大小为0的新数组.

Also you have put it in a while loop which means you create a new array of 0 size on every loop cycle.

尝试将 i = 10(或您的程序逻辑所说的任何内容)并将数组创建排除在循环之外

try putting i = 10 (or whatever your program logic says) and put the array creation out of the loop

相关文章