Results 1 to 9 of 9
  1. #1
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Using scanner to scan multiple user inputs at once

    This question is about Netbeans.

    If someone runs a program, imagine it asks "Please enter a value:, " the user types an integer, and it asks that five times and the user enters five random integers. I want to then display a statement saying "Here are the numbers you entered: <first number>, <second number>, etc."

    Any information about how to do this will be appreciated.
    Last edited by Anne; 08-17-2015 at 12:57 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using scanner to scan multiple user inputs at once

    Use a loop to get the user's input and a println() statement to display it.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Using scanner to scan multiple user inputs at once

    Quote Originally Posted by Anne View Post
    This question is about Netbeans.
    Actually, it's not. It is a basic question about Java. Netbeans is simply the tool you use to facilitate developing Java programs. Netbeans questions would be more along the line of how to I configure my build path? How do I configure editor short cuts? etc.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Using scanner to scan multiple user inputs at once

    I tried this code but it didn't work. How to use a loop to achieve this?

    Java Code:
     Scanner sc = new Scanner (System.in); 
            
            System.out.println ("Enter the number of integers you want to enter."); 
            
            int noofnumbers = sc.nextInt(); 
                  
            for (int a = 0; a < noofnumbers; a++) {
            
                int x = sc.nextInt();
                System.out.println ("Please enter a value for " + a + ": "); 
             
            
                if (a == noofnumbers - 1) {
               
                 System.out.println ("Here is the unsorted list" + x); 
            }
    All the code above displays as the last sentence "Here is the unsorted list <the last integer the user typed>".
    Last edited by Anne; 08-17-2015 at 01:32 AM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using scanner to scan multiple user inputs at once

    Please edit your post and wrap your code in code tags to preserve formatting and highlight the code.

    the code above displays as the last sentence
    The code needs to save all the user's input. There are a couple of ways to do that.
    1) Use an array to hold the user's input
    2) Put the user's input into a String and concatenate each new input to the end of the String.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Using scanner to scan multiple user inputs at once

    How should I hold the user's input in an array? In an array won't I need separate variables for every number the user types:

    int [] numbers = {...};

    But how can I create a code that will assign a variable for all the numbers the user types? Using the for loop, I can only make one variable as I did in the other post. How can I make this variable hold all the numbers the user types? Or is there another way?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using scanner to scan multiple user inputs at once

    If you know the number of inputs the user will make, you can create an empty array of that size and then in the loop store each input as it is read into the array.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Using scanner to scan multiple user inputs at once

    Quote Originally Posted by Norm View Post
    If you know the number of inputs the user will make, you can create an empty array of that size and then in the loop store each input as it is read into the array.
    Actually, the number of inputs the user makes is decided by themselves - they enter a number and then the "Please enter a value" message comes those many times. How should I create an empty array of a variable size? What code should I use? I couldn't find how to do this online.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using scanner to scan multiple user inputs at once

    Use the new statement to create an array with a variable holding the desired size:
    Java Code:
      int[] theArray =  new int[theSize];   // create an int array with theSize elements
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: 05-09-2013, 08:51 AM
  2. Replies: 1
    Last Post: 10-24-2012, 12:21 PM
  3. Replies: 1
    Last Post: 03-19-2012, 01:11 AM
  4. Scanner type inputs
    By erthbound0 in forum New To Java
    Replies: 1
    Last Post: 12-09-2011, 07:51 PM
  5. how to use Scanner to scan a String
    By RichersooN in forum New To Java
    Replies: 1
    Last Post: 09-23-2011, 05:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •