Link to home
Create AccountLog in
Avatar of nsing991
nsing991Flag for United States of America

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?
Avatar of for_yan
for_yan
Flag of United States of America image

You prut them into the ArrayList<Integer> ar = new ArrtayList<Integer>();
ar.add(inieger);

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);

}

}

Open in new window

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]

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of nsing991

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 + "  ");
}

Open in new window

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

Besides in your code I'm not sure they will accumulate
   refStringVal = Integer.toString(referenceString);

Open in new window

this will replace previous one
And 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]);

Open in new window

I agree with for_yan  -- use ArrayList  (no points please).
hmccurdy,

Thanks for the support.
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.