PDA

Click to See Complete Forum and Search --> : Creating/Loading text files [resolved]


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!

System_Error
Apr 2nd, 2007, 02:40 PM
It's working fine for me. Maybe these class files aren't in the same directory and the second program is looking in the wrong place for the text file.


Please give the error you are receiving so we can diagnose what's happening.

lonlonmalon
Apr 3rd, 2007, 10:11 AM
The error I receive is:

Exception in thread "main" java.io.FileNotFoundException: randroll.txt (the system cannot find the file specified)

I think you may have the problem right though, I am fairly new to having two projects work together in this manner. How can I make it so the second program can read the first program's file? Do I just put them in the same workspace or something?

I am, unfortunately for me, fairly new to Java. :sick:

TBeck
Apr 3rd, 2007, 10:25 AM
If you move all of the class files and txt files into the same directory/folder it should be able to find them

lonlonmalon
Apr 4th, 2007, 02:29 PM
Thanks TBeck and System Error, I put all of the java and source files into one file and finally have some success. You people are awesome! :D