import java.util.*;
import java.io.*;
public class Project6
{
public static void main(String[] args)
{
ObjectOutputStream outputStream=null;
ObjectInputStream inputStream=null;
Random generator= new Random();
Random[] y= new Random [20];
try
{
outputStream= new ObjectOutputStream(new FileOutputStream("numbers.dat"));
inputStream= new ObjectInputStream(new FileInputStream("numbers.dat"));
int number;
int i;
int [] x= new int[20];
int r= generator.nextInt(51);
for(i=1; i<=x.length;i++)
{
outputStream.writeInt(generator.nextInt(51));
}
System.out.println("Numbers written to the file numbers.dat.");
outputStream.close();
try
{
while(true)
{
number=inputStream.readInt();
//read 'number' into array 'x' here
System.out.println(number);
}
}
catch(EOFException e)
{
System.out.println("No more numbers in file");
}
//this loop should count frequency of how many times a number occur in array 'x'
int count=0;
for(i=0;i<x.length;i++)
{ count=1;
for(int j=0; j<x.length;j++)
{
if(x[i]== x[j]){
count++;}
}
System.out.println(x[i]);
}
}
catch(IOException e)
{
System.out.println("Problem with file output");
}
}
}
Storing random numbers into an array
Page 1 of 13 Replies - 1109 Views - Last Post: 21 March 2012 - 04:45 PM
#1
Storing random numbers into an array
Posted 21 March 2012 - 02:14 PM
Hi I'm trying to write a program where it generates 20 numbers in between 1-50 then it displays the frequency of the numbers after it. I have it where it displays the random numbers and I have the code for the frequency part written. The only problem I'm having is the 20 random numbers aren't being stored into an array. I know this because when it displays the frequency it comes up all zeros. Here's the code.
Replies To: Storing random numbers into an array
#2
Re: Storing random numbers into an array
Posted 21 March 2012 - 02:29 PM
There's a bit of confusion here. Only one Random class is needed. The main part would be
final int HOW_MANY = 20;
int[] x = new int[HOW_MANY];
Random n = new Random();
for(int i = 0;i < HOW_MANY;i++) {
x[i] = 1 + n.nextInt(49);
}
// Now serialize
This post has been edited by g00se: 21 March 2012 - 02:30 PM
#3
Re: Storing random numbers into an array
Posted 21 March 2012 - 04:32 PM
Okay I got it to store the numbers into the array. Now I'm having trouble with the frequency count. I guess the code I wrote for the frequency count just displays my array?
Here's what I have.
Here's what I have.
import java.util.*;
import java.io.*;
public class Project6
{
public static void main(String[] args)
{
ObjectOutputStream outputStream=null;
ObjectInputStream inputStream=null;
Random generator= new Random();
try
{
outputStream= new ObjectOutputStream(new FileOutputStream("numbers.dat"));
inputStream= new ObjectInputStream(new FileInputStream("numbers.dat"));
int number;
int i;
int [] x= new int[20];
int r= generator.nextInt(51);
for(i=0;i<x.length;i++)
{
x[i]=generator.nextInt(51);
}
for(i=1; i<=x.length;i++)
{
outputStream.writeInt(generator.nextInt(51));
}
System.out.println("Numbers written to the file numbers.dat.");
outputStream.close();
int count=0;
for(i=0;i<x.length;i++)
{ count=1;
for(int j=0; j<x.length;j++)
{
if(x[i]== x[j]){
count++;}
}
System.out.println(x[i]);
}
}
catch(IOException e)
{
System.out.println("Problem with file output");
}
}
}
#4
Re: Storing random numbers into an array
Posted 21 March 2012 - 04:45 PM
If you want to record frequencies, you need another array big enough (51 elements) to store your frequency range (0 to 50 inclusive)
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|