lonlonmalon
Apr 2nd, 2007, 01:59 PM
Alright, so I have two independant programs that are supposed to eventually work together in creating and loading a text file. The first program makes 10 random numbers between 1 and 6, then saves them into a text file. The second program loads the text file, takes the information and presents it to the user as how many times each number between 1 and 6 was generated. However, the second program comes up with an error....
Here's the first program:
import java.io.* ;
public class RandomRoll {
public static void main(String[] args) throws IOException{
int randomNum;
PrintWriter fileOut = new PrintWriter (new FileWriter ("randroll.txt"));
for (int pass = 1; pass <= 10; pass++)
{
randomNum = (int) (Math.random() * 6) + 1;
System.out.println("Roll " + pass + " is a " + randomNum + ".");
fileOut.println (randomNum);
}
fileOut.close();
}
}
and the second:
import java.io.* ;
public class TimesRolled{
public static void main (String[] args) throws IOException {
int randRoll [] = new int [10];
int timesRolled = 0;
BufferedReader readFile = new BufferedReader (new FileReader ("randroll.txt"));
for (int pass = 0; pass < 10; pass++ )
{
randRoll [pass] = Integer.parseInt(readFile.readLine ());
}
for (int x = 1; x <= 6; x++)
{
timesRolled = timeCalculation(randRoll, x);
System.out.print("The number " + x + " has been rolled ");
System.out.println(timesRolled + " times.");
}
}
public static int timeCalculation (int randRoll [], int curNumber)
{
int rollTotal = 0;
for (int i = 0; i < 10; i++)
{
if (randRoll [i] == curNumber)
rollTotal = rollTotal + 1;
}
return rollTotal;
}
}
Thanks for all the help!
Here's the first program:
import java.io.* ;
public class RandomRoll {
public static void main(String[] args) throws IOException{
int randomNum;
PrintWriter fileOut = new PrintWriter (new FileWriter ("randroll.txt"));
for (int pass = 1; pass <= 10; pass++)
{
randomNum = (int) (Math.random() * 6) + 1;
System.out.println("Roll " + pass + " is a " + randomNum + ".");
fileOut.println (randomNum);
}
fileOut.close();
}
}
and the second:
import java.io.* ;
public class TimesRolled{
public static void main (String[] args) throws IOException {
int randRoll [] = new int [10];
int timesRolled = 0;
BufferedReader readFile = new BufferedReader (new FileReader ("randroll.txt"));
for (int pass = 0; pass < 10; pass++ )
{
randRoll [pass] = Integer.parseInt(readFile.readLine ());
}
for (int x = 1; x <= 6; x++)
{
timesRolled = timeCalculation(randRoll, x);
System.out.print("The number " + x + " has been rolled ");
System.out.println(timesRolled + " times.");
}
}
public static int timeCalculation (int randRoll [], int curNumber)
{
int rollTotal = 0;
for (int i = 0; i < 10; i++)
{
if (randRoll [i] == curNumber)
rollTotal = rollTotal + 1;
}
return rollTotal;
}
}
Thanks for all the help!