Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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 Posts: 14Member
    : 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.