2

I am a complete newbie to Java programming, I want to dynamically create objects in Java during run time, I have checked the forms and tried some code but nothing really seems to work.

here is my code .. all help is really appreciated :)

   import java.util.Scanner;

   public class Main{
   public static void main(String[] args){
   String carName;
   String carType;
   String engineType;
   int limit;

  Scanner in = new Scanner(System.in);
  System.out.print("Enter the number of Cars you want to add - ");
  limit = in.nextInt();

    for(int i = 0; i <limit; i++){

    Cars cars[i] = new Cars();

    System.out.print("Enter the number of Car Name - ");
    carName = in.nextLine();

    System.out.print("Enter the number of Car Type - ");
    carType = in.nextLine();

    System.out.print("Enter the Engine Type - ");
    engineType = in.nextLine();

    cars[i].setCarName(carName);
    cars[i].setCarType(carType);
    cars[i].setEngineeSize(engineType);
    String a = cars[i].getCarName();
    String b = cars[i].getCarType();
    String c = cars[i].getEngineeSize();
    System.out.println(a,b,c);

  } 
  }
  }

The cars class looks like this ..

    public class Cars{
    public String carName;
    public String carType;
    public String engineeSize;

    public void Cars(){
    System.out.println("The Cars constructor was created ! :-) ");
    }

    public void setCarName(String cn){
    this.carName = cn;
    }

    public void setCarType(String ct){
    this.carType = ct;

    }

    public void setEngineeSize(String es){
    this.engineeSize = es;

    }

    public String getCarName(){
    return this.carName;
    }



    public String getCarType(){
    return this.carType;
    }

    public String getEngineeSize(){
    return this.engineeSize;
    }


    }

1 Answers1

0

You're on the right track, there are a few errors and unnecessary bits though.

The Cars Class

Your Cars class was mostly fine, (though in my opinion Car would have made more sense) however your constructor didn't make sense, you had public void Cars(), void means "this method returns nothing", but you want to return a Cars object, meaning your constructor needs to become:

public Cars()
{
    System.out.println("The Cars constructor was created ! :-) ");
}

Your Main Class

You were very close here as well, your primary issue was creating the cars array limit times:

for(int i = 0; i < limit; i++)
{
    Cars cars[i] = new Cars();
    //Other code
}

The array needs to be made outside the for loop.

Here is the revised Main class in full, the comments should explain fairly well what I did and why.

import java.util.Scanner;

public class Main{

public static void main(String[] args){
    //The strings here were unnecessary
    int limit;

    Scanner in = new Scanner(System.in);

    System.out.print("Enter the number of Cars you want to add - ");
    limit = in.nextInt();
    in.nextLine(); //nextInt leaves a newLine, this will clear it, it's a little strange, but it makes sense seeing as integers can't have newlines at the end

    //Make an array of Cars, the length of this array is limit
    Cars[] cars = new Cars[limit];

    //Iterate over array cars
    for(int i = 0; i < limit; i++)
    {
        //Read all the properties into strings
        System.out.println("Enter the number of Car Name - ");
        String carName = in.nextLine();

        System.out.println("Enter the number of Car Type - ");
        String carType = in.nextLine();

        System.out.println("Enter the Engine Type - ");
        String engineType = in.nextLine();

        //Set the object at current position to be a new Cars
        cars[i] = new Cars();

        //Adjust the properties of the Cars at this position
        cars[i].setCarName(carName);
        cars[i].setCarType(carType);
        cars[i].setEngineeSize(engineType);

        //We still have the variables from the scanner, so we don;t need to read them from the Cars object
        System.out.println(carName+carType+engineType);
    } 
    in.close(); //We don't need the scanner anymore
   }
}

Finished typing this and realized that the question is two years old :)