在数组中输入数据并显示null时出现程序错误 [英] program error when enter data in array and display null

查看:86
本文介绍了在数组中输入数据并显示null时出现程序错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里引用了一些修正,但仍然完全满足我的程序,但仍然没有完成。

问题1 =我的displayReg()出现nullnullnullnull ...,我应该使用arraylist吗?

问题2 =我需要选择通过IC和NAME搜索searchReg()我该怎么做?

问题3 =需要显示displayReg on .txt文件。

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

        }
    }


}

这是我的Car类

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()+"\tName: "+getName()+"\tIc: "+getIc()+
               "\tPlateNo: "+carInfo.getPlateNum()+"\tColor: "+carInfo.getColor()+"\tYear: "+carInfo.getYear()+
                       "\tMake: "+carInfo.getMake()+"\tModel: "+carInfo.getModel()+"\tCapacity: "+carInfo.getCapacity();
   }

}


推荐答案

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

您实际上是在尝试创建0大小的数组。但是当你尝试在i = 0时访问[i]时,它会尝试访问数组中第1个位置的元素(数组有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

这篇关于在数组中输入数据并显示null时出现程序错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆