0

Everything works except it's not giving me what I want. Where it says 'list.add(ln)', all it does is give me a boolean that says true.

Is there any way to store what I wrote from the keyboard into the array then print it out?

  ArrayList<Integer> list = new ArrayList<Integer>();

        ln = 1;     // prime the loop   

        while(ln != 0) { // begin while ln doesnt equal to zero

            System.out.println("\nFor list[" + n + "], enter a number.");
            ln = console.readInt();

            if (ln != 0){ // begin if the input from keyboard does not equal to zero

                System.out.println("Array " + list.add(ln));

                n++;    // update array size                    

            } // end if the input from keyboard does not equal to zero

            else

                System.out.println();

        } // end while ln doesnt equal to zero
3
  • What does ArrayList#add(..) do? Go through the javadoc. Commented Apr 17, 2014 at 4:44
  • Java has a documentation tool called javadoc. Use it when you don't know what JDK methods do. Commented Apr 17, 2014 at 4:45
  • u have Make list of Integar which is basically enter object of Integar. and u have to make for loop to retrieve this list. Commented Apr 17, 2014 at 4:49

3 Answers 3

1

This is because you are adding it on System.out.println which means that you are cheking a boolean that can be either true or false if you can enter it.

If you add it to the list and then print the index it would work:

if (ln != 0)
{ 
    list.add(ln);
    System.out.println("Array " list.get(n));
    n++;                        
} 
Sign up to request clarification or add additional context in comments.

Comments

1

You have to iterate through array that you have created..It has already stored what you have entered..method just give acknowledge that it is successfully stored..

 for (int x = 0; x < list.size(); x++)
        {
        System.out.println("Array " +list.get(x)); 
        }

Comments

1
 ArrayList<Integer> list = new ArrayList<Integer>();
    Scanner sc = new Scanner(System.in);
    do {

       Integer i=sc.nextInt();
       list.add(i);

     }while (i!=0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.