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 6 of 6

Thread: advice needed on storing string into arraylist and printing it out.

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default advice needed on storing string into arraylist and printing it out.

    Hi guys,

    thanks for reading my post.

    I am trying to store a user input of string into the arraylist and after storing all the user string input in the arraylist. i would like to print the content of each string index in the arraylist. Any advice would be appreciated.

    import java.util.Scanner;
    import java.util.ArrayList;
    public class EchoStrings{

    public static void main (String[] args){


    ArrayList<String> stringList = new ArrayList<String>();// arraylist of string

    String str = "start";//starting condition to make while loop run



    Scanner kb = new Scanner(System.in);



    do {

    System.out.println("enter string:");
    str=kb.next();

    stringList.add(str);











    }while(!str.equals("stop"));




    for (int j =0;j<stringList.size();j++){

    System.out.println(stringList.get(j));


    }



    }




    }

    my programs can compile but it just cant continue after the while loops it stops at the do while loop. Any advice is much appreciated. Thank You


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: advice needed on storing string into arraylist and printing it out.

    It works for me. Output of a run from a command line:

    enter string:
    Four
    enter string:
    score
    enter string:
    and
    enter string:
    seven
    enter string:
    years
    enter string:
    ago
    enter string:
    stop
    Four
    score
    and
    seven
    years
    ago
    stop


    How are you running it?
    • From a command line?
    • From within an Integrated Development Environment? (Which IDE?)
    • What?



    Cheers!

    Z

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: advice needed on storing string into arraylist and printing it out.

    i am using a jcreator. but i have no idea why it cant work. u mean u copy paste my code and it is working? if its working it means im doing my question correctly but i cant figure out why it stop at the while loop. which ide are u using to run my code.

    Thank You.

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: advice needed on storing string into arraylist and printing it out.

    Hi its working now . thanks. any idea How do i make the while loop stop when the user press the enter key?

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: advice needed on storing string into arraylist and printing it out.

    Quote Originally Posted by jong2009 View Post
    ...while loop stop when the user press the enter key?
    You could, maybe, use nextLine() instead of next().

    Try the following in your while loop and see what happens when you press 'Enter' and nothing else when prompted:

            do {
                System.out.println("enter string:");
                str = kb.nextLine();
                System.out.println("str.length() = " + str.length());
    .
    .
    .


    Cheers!

    Z

  6. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: advice needed on storing string into arraylist and printing it out.

    thanks for the advice. managed to make it work.

    cheers

Similar Threads

  1. [SOLVED] Printing arraylist contents in Jtextarea
    By Johnny Bravo in forum AWT / Java Swing
    Replies: 4
    Last Post: August 31st, 2012, 08:56 AM
  2. private Map<String, ArrayList> xlist = new HashMap<String, ArrayList>();
    By Scotty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 21st, 2011, 08:37 AM
  3. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM
  4. Searching and printing string results from an Arraylist. Having difficulty.
    By Espressoul in forum Loops & Control Statements
    Replies: 1
    Last Post: February 25th, 2010, 08:32 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM