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

Thread: question about getting data from the console

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default question about getting data from the console

    I'm trying to fill an arraylist with number but I don't want the user to have to press enter after each number. I would like it if they could put all the numbers on one line separated by white space like so.

    Enter initial candy counts: 23 32 43 5 6 54

    here what I have so far.

    also if anyone can point me in the right direction about how to end the loop after they press enter would be much appreciated.

    thank you.
     
            Scanner userInput = new Scanner(System.in);
            ArrayList<Integer> candy = new ArrayList<Integer>();
     
            int numCandy = 0;
     
            while() {  
     
                System.out.println("Enter initial candy counts: ");
                numCandy = userInput.nextInt();
     
                candy.add(numCandy);
            }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: question about getting data from the console

    What happens when you compile and execute the code and enter the data that way?
    The Scanner class has methods to test if there is input ready to read. The method names start with has

    Or you could use the nextLine() method to read all of one line into a String and use a Scanner with that String or use the String class's split() method to get the data.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: question about getting data from the console

    Thanks for the idea. But now i cant figure out why the numbers are still strings. any ideas

     
            Scanner userInput = new Scanner(System.in);
            ArrayList<Integer> candy = new ArrayList<Integer>();
     
            String temp;
            String[] candyNum;
     
            System.out.print("Enter initial candy counts: ");
            temp = userInput.nextLine();
     
            candyNum = temp.split(" ");
     
            for (int i = 0; i < candyNum.length; i++) {
                candy.add(Integer.parseInt(candyNum[i])); 
           }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: question about getting data from the console

    why the numbers are still strings
    Can you explain what numbers you are talking about? What variable hold the Strings? Where and how would they have been changed to something else?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: question about getting data from the console

    I thought Integer.parseInt(candyNum[i]) would change the string to a int?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: question about getting data from the console

    Why do you think it is not doing that?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: question about getting data from the console

    API parseInt: "Parses the string argument as a signed decimal integer"

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: question about getting data from the console

    What is your question or problem?
    If you don't understand my answer, don't ignore it, ask a question.

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

    thatguy (February 6th, 2013)

  10. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: question about getting data from the console

    OMG i'm so sorry i must of made a mistake when i was testing it. everything works fine thanks for all your help.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: question about getting data from the console

    Glad you got it worked out.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Simple Question - How to Parse a CSV File Into Different Data Type Arrays?
    By Obiwan64 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 07:30 PM
  2. data structures question
    By hwoarang69 in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 4th, 2012, 03:07 PM
  3. Data storage question - which type would be best?
    By mmazur in forum Java Theory & Questions
    Replies: 5
    Last Post: August 25th, 2012, 09:57 AM
  4. Very Basic Question: Reference Data Types
    By Bill123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 14th, 2012, 10:39 PM
  5. Replies: 4
    Last Post: October 24th, 2011, 08:09 AM