Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to store 10 inputs in an arraylist?

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to store 10 inputs in an arraylist?

    I have this code:
    import javax.swing.JOptionPane;
     
    public class Hello{
    public static void main (String args []){
     
    String[] anArray= new String [10];
    for(int i=0; i< anArray.length; i++){
      anArray= JOptionPane.showInputDialog(null, "Enter a name:");
    }
     JOptionPane.showMessageDialog(null, anArray[0]);
    }
    }
    how can i print out the first input which is contained in index [0] of the arraylist where the 10 inputs are supposed to be stored? any help please?

    Please point out the wrong codes or kindly fix my code thanks.


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: How to store 10 inputs in an arraylist?

    Inside the loop you are just continually re-assigning the array to a user input, which doesn't work since a string is not a string array. You use indexing so I assume you understand how to index through an array, why not try using this way of indexing on the changing variable in the loop(i)?

    here is a quick example that only reads an array.
    int[] anArray = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    for(int i = anArray.length; i >= 0; i--){
      System.out.println(anArray[i]);
    }

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to store 10 inputs in an arraylist?

    so in my JOptionPane inside the for loop what should i do with the instantiation for it to contain a value whenever the user inputs

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: How to store 10 inputs in an arraylist?

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to store 10 inputs in an arraylist?

    thanks i've already solved it thru ur recommendations. helps a lot....

Similar Threads

  1. how to give inputs to my simple java program
    By pokuri in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 8th, 2011, 06:36 PM
  2. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  3. Inputs not being applied.
    By Norflok669 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2010, 12:25 AM
  4. How can i store ArrayList objects in Access database
    By frankycool in forum JDBC & Databases
    Replies: 0
    Last Post: November 3rd, 2009, 11:44 PM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM