1

Just learning and the concept of arrays is vague. I know how to prompt for an input;

System.out.println("\nEnter a value for the radius: ");

I can code for the three arrays;

Cylinder[] cylinders = new Cylinder[3]; //creates the individual cylinders
cylinders[0] = new Cylinder(10.0, 5.0); 
cylinders[1] = new Cylinder(11.0, 6.0); 
cylinders[2] = new Cylinder(5.0, 2.0); 

but don't know how to code for the arrays when prompting user for the input and storing in the array. Can anyone show me how the code for this part should look?

Bklyn_40
  • 21
  • 1
  • 1
  • 7

4 Answers4

1

Like so:

Cylinder[] cylinders = new Cylinder[3];

// Loop using a counter variable, commonly called i,
// as many times as you have array elements:
for (int i = 0; i < cylinders.length; i++) {
    // Each time around the loop, ask for the properties of the cylinder
    System.out.println("\nEnter a value for the radius: ");
    double radius = new Scanner(System.in).nextDouble();
    System.out.println("\nEnter a value for the height: ");
    double height = new Scanner(System.in).nextDouble();

    // Set each array element i to the newly created object
    cylinders[i] = new Cylinder(radius, height);
}
Boann
  • 48,794
  • 16
  • 117
  • 146
  • Thank you for the code. I still am not clear how the array part works, but can see how the counter does. How does this accept all 3 inputs for all cylinders THEN give the outputs for the cylinders? Is this because of the Cylinder[3]? How would I get it to display for each one after the inputs are given? System.out.println("Cylinder 1 = "cylinders[0], "/nCylinder 2 = "cylinders[1], "/nCylinder 3 = "cylinders[2]); – Bklyn_40 Sep 24 '13 at 03:07
  • The first line above creates the `cylinders` array of length 3. The [`for` keyword](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html) creates a loop, which means the code inside its `{ ... }` can run multiple times. In this case, it will run three times. The first time, the variable `i` will be 0, the second time `i` will be 1, and the third time it will be 2. Each time through the loop body it will prompt for new `radius` and `height` values, then use them to create a new Cylinder object, and finally it will put the new Cylinder object in the array at position `i`. – Boann Sep 24 '13 at 03:12
  • @Bklyn_40 If you're doing this in an IDE like Eclipse or NetBeans you can "step" through the code, watching it run one line at a time, which should make the behavior easy to understand. To print each cylinder, add this line to the end of the loop body: `System.out.println("Cylinder " + (i + 1) + " = " + cylinder[i]);` – Boann Sep 24 '13 at 03:15
  • Took a bit to see how to use Eclipse to step thru, but I now I get it. Thanks for the advice. Seeing it work makes it easier to understand. I could not find your println format in my book, but wow, it works great. Thank you so much for your time and insight. – Bklyn_40 Sep 24 '13 at 15:01
1

Starting with how to initialize a single cylinder

    //First you need a place to hold the value that the user inputs
    float radius, height;

    //Then you need an object that can read input from the user.
    Scanner sc = new Scanner(System.in);

    //Tell the user what you are expecting them to do
    System.out.println("Enter radius: ");
    //Now use the object to read in a value (You will need to convert it)
    radius = sc.nextFloat();

    //Tell the user what you are expecting them to do
    System.out.println("Enter height: ");
    //Now use the object to read in a value (You will need to convert it)
    height = sc.nextFloat();

    Cylinder cylinders = new Cylinder(radius, height); //creates the individual cylinders

Now how do you initialize an array of cylinders. Arrays go hand-in-hand with for loops. so we take the code above and wrap it in a for loop like so.

    Cylinder[] cylinders = new Cylinder[3];

    //Then you need an object that can read input from the user.
    // No reason to create it multiple times so create it outside the loop.
    Scanner sc = new Scanner(System.in);

    for (int i = 0; i < cylinders.length; i++) {
        //First you need a place to hold the value that the user inputs
        float radius, height;



        //Tell the user what you are expecting them to do
        System.out.println("Enter radius for cylinder[" + i + "]: ");
        //Now use the object to read in a value (You will need to convert it)
        radius = sc.nextFloat();

        //Tell the user what you are expecting them to do
        System.out.println("Enter height for cylinder[" + i + "]: ");
        //Now use the object to read in a value (You will need to convert it)
        height = sc.nextFloat();

        cylinders[i] = new Cylinder(radius, height); //creates the individual cylinders
    }
Nathaniel Johnson
  • 4,731
  • 1
  • 42
  • 69
  • Note: `double` would seem to be the more correct choice here than `float`, as OP's original code used `double`s, so that must be the type of the parameters to the `Cylinder` constructor. – Boann Sep 23 '13 at 02:10
0

You can accept user input in the form of 'String[] args' argument array to the 'main' method.

Instantiate the cylinder array with args[0], args[1] and so on such as new Cylinder(10.0, args[0]);

Say the name of your java class is Cylinders, it will be called in this fashion - Cylinders 5.0 6.0 2.0

Perhaps what you have is similar to this - Getting User input with Scanner

That will also work.

Community
  • 1
  • 1
adskuser
  • 19
  • 4
0

Ask user to input 3 arguments at one time and splited by space. Once you get 3 arguments, you can call the constructor and new that object.

Use the standard system input method or scanner both is ok. Good luck

Spark8006
  • 635
  • 1
  • 7
  • 15