nsing991
asked on
How do I read randomly created integers to a buffer and how do I print the buffers contents all at once?
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?
Random r = new Random();
ArrayList<Integer> ar = new ArrtayList<Integer>();
int count = 0;
wheil(count < 100) {
ar.add(r.nextInt());
count++
}
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) {
ar.add(r.nextInt());
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]
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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]);
BufferedReader reader = new BufferedReader(new StringReader(refStringVal));
line = reader.readLine();
System.out.print(line + " ");
}
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
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
Besides in your code I'm not sure they will accumulate
And you don't want to read them immediately.
No, I sugest to go with ArrayList.
refStringVal = Integer.toString(referenceString);
this will replace previous oneAnd you don't want to read them immediately.
No, I sugest to go with ArrayList.
I meant this line will replace previous one (in the poasting above)
refStringVal = Integer.toString(referenceString[i]);
I agree with for_yan -- use ArrayList (no points please).
hmccurdy,
Thanks for the support.
Thanks for the support.
ASKER
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!
You are always welcome.
ar.add(inieger);