We help IT Professionals succeed at work.

# How do I read randomly created integers to a buffer and how do I print the buffers contents all at once?

on
I am creating random integers using a random generator.  The number of random integers are created in a for- loop and the number of integers is based on the users input.  How do I store each of the random integers in a buffer?  How do I later read all of the integers stored in the buffer so that they can be printed all at once?
Comment
Watch Question

## View Solution Only

Awarded 2011
Awarded 2011

Commented:
You prut them into the ArrayList<Integer> ar = new ArrtayList<Integer>();
Awarded 2011
Awarded 2011

Commented:

Random r = new Random();
ArrayList<Integer> ar = new ArrtayList<Integer>();
int count = 0;
wheil(count < 100) {

count++

}
Awarded 2011
Awarded 2011

Commented:
This is a real test (see output)

``````public class Miscellaneous {
public static void main(String[] args) {

Random r = new Random();
ArrayList<Integer> ar = new ArrayList<Integer>();
int count = 0;
while(count < 25) {

count++ ;

}

System.out.println(ar);

}

}
``````
Output:
``````[250217660, -321923507, 1573048347, -683200724, -706084161, -911243119, -2109798926, 1481062972, 1754929536, -907595966, 761267923, 793092370, -29241347, 425644272, -541220445, 918826598, -983467479, 1359954747, -1670040178, 689459692, 2010926299, 1472848317, 1588816150, -2063987750, -1457832694]
``````
Awarded 2011
Awarded 2011
Commented:

if we limiet them to less than 100 it wil look more beautiful:
``````import java.util.*;
public class Miscellaneous {
public static void main(String[] args) {

Random r = new Random();
ArrayList<Integer> ar = new ArrayList<Integer>();
int count = 0;
while(count < 25) {

count++ ;

}

System.out.println(ar);
``````
Output:
``````[23, 97, 7, 56, 74, 97, 72, 12, 91, 8, 21, 30, 27, 26, 35, 95, 83, 28, 79, 40, 29, 96, 1, 62, 94]
``````

Commented:
Is an ArrayList considered a buffer?  I was thinking of this approach:
``````for (int i = 0; i < stringLength; i++){
referenceString[i] = generator.nextInt(8);
refStringVal = Integer.toString(referenceString[i]);
System.out.print(line + "  ");
}
``````
Awarded 2011
Awarded 2011

Commented:
ArrayList is no worse a buffer
I 'd think it is a better and more ocnvenient buffer
beacuse you don't spend resources to convert them to string
For me ArrayList looks much more convenient

Awarded 2011
Awarded 2011

Commented:
Besides in your code I'm not sure they will accumulate
``````   refStringVal = Integer.toString(referenceString);
``````
this will replace previous one
And you don't want to read them immediately.
No, I sugest to go with ArrayList.
Awarded 2011
Awarded 2011

Commented:

I meant this line will replace previous one  (in the poasting above)
``````   refStringVal = Integer.toString(referenceString[i]);
``````

Commented:
I agree with for_yan  -- use ArrayList  (no points please).
Awarded 2011
Awarded 2011

Commented:
hmccurdy,

Thanks for the support.

Commented:
Ok, it makes sense.  This is exactly the problem that I'm having when I later try to print the entire string.  Only the last value that was generated is printed.  I will try the ArrayList approach.  Thanks!
Awarded 2011
Awarded 2011

Commented:
You are always welcome.