1

Basically every time the for loop runs, I want to create an array with a new name. So at the end of the program I'll be able to print out each array containing all the individual variables within. Here is my code so far with an array created however each time the loop statement is ran it is over writing the array.

package clark.motors.car.check.program;

import java.util.*;

public class ClarkMotorsCarCheckProgram {

    public static void main(String[] args) 
    {


        //System.out.println(test);
        Scanner Input = new Scanner(System.in);
        int inputcars = 0;
        //List<String> reg3 = new ArrayList<>();

        boolean newcar=false;
        //int inputcars;

        System.out.println("Welcome to Clarke's Automobile Car-Check Program");
        System.out.println("Please specify 'Yes' or 'no' if you wish to add a new char to the current list");
        System.out.println("");
        String text = Input.nextLine();
        if(text.equals("yes"))
        {
            System.out.println("Please specify How many you wish to add");
            System.out.println("");
            inputcars = Input.nextInt();
            Input.nextLine();
            //int u = inputcars;
            //String strI = "" + u;
        }
        for (int i=0;i<inputcars;i++)
        {
            String strI = "" + i;
            String test = "person" + i;
            Scanner Input2 = new Scanner(System.in);
            System.out.println("Please specify the Regristration of the next car");
            String reg = Input2.nextLine();

            System.out.println("Please specify the color of the next car");
            String color = Input2.nextLine();

            System.out.println("Please specify how many wheel are on the nect car");
            int wheels = Input2.nextInt();

            System.out.println("Please specify how many doors are on the next car");
            int doors = Input2.nextInt();

            System.out.println("Please specify the make of the next car");
            String make = Input2.nextLine();
            String make2 = Input2.nextLine();
            System.out.println("Please specify the state of the next car. 'scrap', 'repairs', 'working'");
            String state = Input2.nextLine();

            // ClarkMotarsCreateCars reg2  = new ClarkMotarsCreateCars(reg,color,wheels,doors,make,state);
            //Map<String, String> details = new HashMap<>();

            //List<String> details = new ArrayList <> ();
        String details [] = {reg, color, make, state}; 
        int car2 [] = {wheels, doors};
        System.out.println("The reg of the car is: "+reg);
        System.out.println("The colour of the car is: "+color);
        System.out.println("The make of the car is: "+make2);
        System.out.println("The state of the car is: "+state);
        System.out.println("The amount of wheels on the car: "+wheels);
        System.out.println("The amount of doors on the car: "+doors);
        }

    }
    public String details [] = {reg, color, make, state}; 

}

1 Answers1

0
ArrayList<ClarkMotarsCreateCars> cars = new ArrayList<>();

for(..){
     cars.add(new ClarkMotarsCreateCars(reg,color,wheels,doors,make,state));
}

This will give you an array of objects and not overwrite the first entry.

blur0224
  • 972
  • 8
  • 26
  • This works great, however is there now an easy way to print this out, so allowing us to print out the first object in the first list in the array – JamesGratrix Nov 13 '15 at 10:02
  • http://stackoverflow.com/questions/13001427/printing-out-all-the-objects-in-array-list – blur0224 Nov 13 '15 at 14:37