-3

Hi I'm working on a java program that allows user to input their information such as name, car model, capacity etc, later they can see their information in a list:

Car Registration Listing

Reg No. Name IC No. Plate No. Color Year Make Model Capacity

1001 John Wayne 111111111 ABC123 Blue 2010 Toyota Vios 1.5

1002 Bea Arthur 222222222 WEA888 Red 2010 Nissan Teana 2.0

1003 Meg Ryan 333333333 PBL168 Black 2011 Honda City 1.6

1004 Jane Doe 444444444 BBB777 White 2011 Nissan Teana 2.0

1005 Al Johnson 555555555 CAT118 Green 2012 Toyota Vios 1.5

1006 Ned Beatty 666666666 TV798 Blue 2012 Toyota Vios 1.5

Below is the code:

public class CarRegistrationListing {
    
        int regNo;
        String name;
        int icNo;
        String plateNo;
        String color;
        int year;
        String make;
        String model;
        double capacity;
    
        public CarRegistrationListing(int regNo, String name, int icNo, String plateNo, String color, int year, String make, String model, double capacity) {
            this.regNo = regNo;
            this.name = name;
            this.icNo = icNo;
            this.plateNo = plateNo;
            this.color = color;
            this.year = year;
            this.make = make;
            this.model = model;
            this.capacity = capacity;
        }
    
        public int getRegNo() {
            regNo++;
            return regNo;
        }
        
        public String getName(){
            return name;
        }
        
        public int getIcNo(){
            return icNo;
        }
        
        public String getPlateNo(){
            return plateNo;
        }
        
        public String getColor(){
            return color;
        }
        
        public int getYear(){
            return year;
        }
        
        public String getMake(){
            return make;
        }
        
        public String getModel(){
            return model;
        }
        
        public double getCapacity(){
            return capacity;
        }
        
        public void setName(String name){
            this.name = name;
        }
        
        public void setIcNo(int icNo){
            this.icNo = icNo;
        }
        
        public void setPlateNo(String plateNo){
            this.plateNo = plateNo;
        }
        
        public void setColor(String color){
            this.color = color;
        }
        
        public void setYear(int year){
            this.year = year;
        }
        
        public void setMake(String make){
            this.make = make;
        }
        
        public void setModel(String model){
            this.model = model;
        }
        
        public void setCapacity(double capacity){
            this.capacity = capacity;
        }
    
        @Override
        public String toString() {
            return "CarRegistrationListing{" + "regNo=" + regNo + ", name=" + name + ", icNo=" + icNo + ", plateNo=" + plateNo + ", color=" + color + ", year=" + year + ", make=" + make + ", model=" + model + ", capacity=" + capacity + '}';
        }
    
    }

This is for test run:

import java.util.Scanner;

public class TestCarReg {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.print("How many records do you want to register: ");
    int record = scan.nextInt();

    CarRegistrationListing car = new CarRegistrationListing(1000, "", 0, "", "", 0, "", "", 0.0);

    for (int i = 0; i < record; i++) {
        String[] carType = {"Toyota Vios", "Nissan Teana", "Honda City"};
        System.out.println("Car Type:");
        for (String carType1 : carType) {
            System.out.println(carType1);
        }

        System.out.print("Choose your car type(1 to 3): ");
        int type = scan.nextInt();
        String make;
        String model;
        switch (type) {
            case 1:
                make = "Toyota";
                model = "Vios";
                break;

            case 2:
                make = "Nissan";
                model = "Teana";
                break;

            default:
                make = "Honda";
                model = "City";
                break;

        }

        System.out.print("Enter name: ");
        String name = scan.nextLine();
        car.setName(name);
        scan.nextLine();
        System.out.print("Enter IC number: ");
        int icNo = scan.nextInt();
        car.setIcNo(icNo);
        System.out.print("Enter plate number: ");
        String plateNo = scan.next();
        car.setPlateNo(plateNo);
        System.out.print("Enter color: ");
        String color = scan.next();
        car.setColor(color);
        System.out.print("Enter year: ");
        int year = scan.nextInt();
        car.setYear(year);
        car.setMake(make);
        car.setModel(model);
        System.out.print("Enter capacity: ");
        double capacity = scan.nextDouble();
        car.setCapacity(capacity);
    }

    System.out.println("Car Registration Listing");
    for (int i = 0; i < record; i++) {
        System.out.println("Reg No.\tName\t\tIC No.\tPlate No.\tColor\tYear\tMake\tModel\tCapacity");
        System.out.println(car.getRegNo() + "\t" + car.getName() + "\t\t" + car.getIcNo() + "\t" + car.getPlateNo() + "\t\t" + car.getColor() + "\t" + car.getYear() + "\t" + car.getMake() + "\t" + car.getModel() + "\t" + car.getCapacity());
    }

}

}

I tried printing the output according to the sample shown above, but I got this in my output:

debug: How many records do you want to register: 2

Car Type: Toyota Vios Nissan Teana Honda City

Choose your car type(1 to 3): 1

Enter name: william Sebastian

Enter IC number: 1111

Enter plate number:AC1212

Enter color: Red

Enter year: 2010

Enter capacity: 1.0

Car Type: Toyota Vios Nissan Teana Honda City

Choose your car type(1 to 3): 1

Enter name: wong ang soon

Enter IC number: 2222

Enter plate number: AR1234

Enter color: White

Enter year: 2013

Enter capacity: 2.0

Reg No. Name IC No. Plate No. Color Year Make Model Capacity

1001 2222 AR1234 White 2013 Honda City 2.0

Reg No. Name IC No. Plate No. Color Year Make Model Capacity

1002 2222 AR1234 White 2013 Honda City 2.0

The name can't be printed and the variables for first person were disappeared, I tried to use array for CarRegistrationListing but it caused error. I would appreciate if someone can point out my mistakes. Thank you so much!

brombeer
  • 8,716
  • 5
  • 21
  • 27
LIM WENG NI
  • 33
  • 1
  • 4
  • 4
    Are you working on JAVA of PHP!? your title says JAVA and you have added a PHP tag. – Bhaumik Pandhi Aug 02 '22 at 06:00
  • 1
    Use ArrayList for these kind task, it will be more efficient. – Jagadish S Aug 02 '22 at 06:01
  • @BhaumikPandhi, hi sorry I put the wrong tag, I worked on Java – LIM WENG NI Aug 02 '22 at 06:02
  • 2
    part of the problem (missing name) is handled by the following question: [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045/16320675) || another part (missing first entry) is also common on StackOverflow, but I am too lazy to search it: code is creating just one instance for all entries (`new CarRegistrationListing`), before the loop - so, inside the loop, the same instance is changed (like changing the plate, color, owner of the car, but not *producing* a new car) - there is no second (3rd, ...) instance to hold the data of all cars – user16320675 Aug 02 '22 at 06:13
  • By the way, a tip: [records](https://openjdk.org/jeps/395). `public record CarRegistrationListing( int regNo, String name, int icNo, String plateNo, String color, int year, String make, String model, double capacity ) {}` can be your *entire* class declaration if you don't need setters. – Basil Bourque Aug 13 '22 at 22:50

1 Answers1

1

the variables for first person were disappeared

It is because the variable car is overwritten inside the for loop every time when you enter a new entry. To show the values for first person, you can create an ArrayList before the data-entry for loop

List<CarRegistrationListing> carList = new ArrayList<CarRegistrationListing>();

And store the values into the ArrayList at the end of for loop

carList.add(car);

At last, you may print the values from ArrayList as below.

System.out.println("Reg No.\tName\t\tIC No.\tPlate No.\tColor\tYear\tMake\tModel\tCapacity");
for (CarRegistrationListing car : carList) {
    System.out.println(car.getRegNo() + "\t" + car.getName() + "\t\t" + car.getIcNo() + "\t" + car.getPlateNo() + "\t\t" + car.getColor() + "\t" + car.getYear() + "\t" + car.getMake() + "\t" + car.getModel() + "\t" + car.getCapacity());
}

The name can't be printed

It is because the newline character is not consumed by nextInt() causing input not properly read, you can add scan.nextLine() before reading the name from input.

You may find more information from here, as mentioned by @user16320675 in the comments

scan.nextLine();
String name = scan.nextLine();

I also noticed you would like to have autoincrement for RegNo.

Instead of adding regNo++ in getter function, I will suggest to create a static member for generating unique regNo inside Constructor

More information at: Java create a unique ID for each instantiated object using instance methods instead of class/static methods

static int globalRegNo = 1000;

public CarRegistrationListing(int regNo, String name, int icNo, String plateNo, String color, int year, String make, String model, double capacity) {
    this.regNo = globalRegNo++;
    this.name = name;
    this.icNo = icNo;
    this.plateNo = plateNo;
    this.color = color;
    this.year = year;
    this.make = make;
    this.model = model;
    this.capacity = capacity;
}
charmian
  • 506
  • 2
  • 9