Results 1 to 5 of 5

Thread: Creating/Loading text files [resolved]

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    9

    Creating/Loading text files [resolved]

    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:
    Code:
     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:

    Code:
     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!
    Last edited by lonlonmalon; Apr 4th, 2007 at 02:30 PM. Reason: resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width