Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Load user input from console into an object array and read it

Hi, I am a new java developer and need help with this.

I need to capture input from user from console and then load it into an object array and then read from the array as I need to read the info and do some calculations on it.

I know how to prompt user and capture it in a variable, but dont know how I can then use this info to load in an array. Please help....

Comments

  • RichardKRichardK Member Posts: 14
    : Hi, I am a new java developer and need help with this.
    :
    : I need to capture input from user from console and then load it into an object array and then read from the array as I need to read the info and do some calculations on it.
    :
    : I know how to prompt user and capture it in a variable, but dont know how I can then use this info to load in an array. Please help....
    :
    Hi

    Your best bet is an ArrayList. This is slightly different to an array in the fact the ArrayList can be resized. I suggest this as you might not be able to predict the amount of input from the console.

    You can add elements into it by the following

    Object fromConsole;

    //create the array list
    ArrayList test = new ArrayList();

    //add element
    test.add(fromConsole);

    //get from ArrayList
    test.get(0);

    //turning it into an array
    Object[] temp = test.toArray();

    //casting to a string from the arraylist
    String tempStr = (String) test.get(0);
    //String tempStr = test.get(0) + ''; //would also work

    //casting to a boolean from the arraylist
    String tempStr = (boolean) test.get(0);

    The API Documentation for Array list is
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html
    not sure how experience you are so the url might not help.

    You might need to import the Array list at the top of your class file.
    import java.util.ArrayList;

    Hope this helps.
Sign In or Register to comment.