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

Thread: Help With coding on Arraylists and Scanner

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help With coding on Arraylists and Scanner

    Hi I'm new to learning java/programing and I'm trying to create a function with the scanner class that reads the next line and appends it to an array list however my code doesn't work the way I want it to:

    import java.util.ArrayList;
    import java.util.Scanner;
     
    public class arrayappendtest {
     
        public static void main(String[] args) {
     
            Scanner s = new Scanner(System.in);
            String tmp;
     
            ArrayList<String> sample = new ArrayList();
     
            while (s.hasNext()) {
                System.out.println("Enter something (type quit to quit): ");
                tmp = s.nextLine();
                if (tmp.equals("quit")){
                    break;
                } else{
                    sample.add(tmp);
                }
            }
            s.close();
            System.out.println(sample);
     
        }
    }

    when I run this block there is always an extra entry


    test // this shouldn't be appended
    Enter something (type quit to quit):
    test1
    Enter something (type quit to quit):
    test2
    Enter something (type quit to quit):
    quit
    Enter something (type quit to quit): // this shouldn't print either
    [test, test1, test2]

    I don't understand why this is happening / what would be the best practice to solve I'm not appending anything until I get to my while loop.

    Thank you

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help With coding on Arraylists and Scanner

    Take a look at this: https://stackoverflow.com/questions/...ore-the-output
    loop won't start until Scanner detects input is available, and so the program blocks waiting for input
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    thefishvish (January 27th, 2022)

Similar Threads

  1. Replies: 1
    Last Post: June 4th, 2021, 06:36 PM
  2. Help in ArrayList of ArrayLists
    By pk_sinha53 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 14th, 2019, 06:17 AM
  3. Replies: 2
    Last Post: February 19th, 2012, 07:36 AM
  4. [SOLVED] ArrayLists - Getting index of parameters of objects of ArrayLists.
    By mwebb in forum Object Oriented Programming
    Replies: 1
    Last Post: February 16th, 2012, 07:39 PM
  5. Creating ArrayLists
    By Khadafi in forum Java Theory & Questions
    Replies: 15
    Last Post: January 12th, 2012, 06:18 PM