Hi, Inorder to get input from user, we will ask user how many number of inputs the us

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RamaLakshmi ERL
    New Member
    • Dec 2015
    • 2

    #1

    Hi, Inorder to get input from user, we will ask user how many number of inputs the us

    In order to get input from user, we will ask user how many number of inputs the user is going to enter. But now my requirement is to allow the user to enter any number of inputs. How can I Perform it???
    One of the way is using while loop.But the problem with while loop is storing the null value. How to initialise the array?
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    read all inputs in a while-loop until the user types "end".

    Do not use static array, but collection framework.
    Then there is no need to initalize the array with a fixed size beforehand.

    before loop:
    Code:
    ArrayList numberList = new ArrayList();
    in loop:
    Code:
    numberList.add(number);
    after loop:
    Code:
    System.out.println("size=" + numberList.size() + ", content=" + numberList);

    Comment

    Working...