Results 1 to 3 of 3
  1. #1
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Help with creating a code about Java files

    We're supposed to create a program that will simulate 10 random dice rolls, which will then be stored in a text file. A second program will be written that will read the data from the previous text file and display on the screen the number of times the numbers 1 to 6 were rolled. it should have arrays and defined methods.

    I finished my code, but it seems to be displaying the wrong number for the number of times a certain number was rolled. can someone please tell me what to fix. Thanks!
    CODE:
    1st program:

    Java Code:
    import java.io.*;
    
    public class Unit1a
    {
    public static void main (String [] args) throws IOException
    {
    int randNum;
    
    PrintWriter fileOut = new PrintWriter (new FileWriter ("randroll.txt"));
    
    for (int i = 0; i < 100; i++)
    {
    randNum = (int)(Math.random()*6) + 1;
    
    fileOut.println (randNum);
    }
    fileOut.close();
    }
    }
    2nd program:

    Java Code:
    import java.io.*;
    
    public class Unit1
    {
    public static void main (String [] args) throws IOException
    {
    BufferedReader readFile = new BufferedReader (new FileReader ("randroll.txt"));
    int random [] = new int [100];
    
    for (int i = 1; i <= 6; i++)
    {
    random [i] = Integer.parseInt (readFile.readLine());
    System.out.println("The number " + i + " was rolled " + (findTimes (random)) + " times.");
    
    }
    }
    
    public static int findTimes (int random [])
    {
    int rollTimes = 0;
    for (int i = 0; i < 100; i++)
    {
    
    if (random[i] == 1)
    {
    rollTimes++;
    }
    if (random[i] == 2)
    {
    rollTimes++;
    }
    if (random[i] == 3)
    {
    rollTimes++;
    }
    if (random[i] == 4)
    {
    rollTimes++;
    }
    if (random[i] == 5)
    {
    rollTimes++;
    }
    if (random[i] == 6)
    {
    rollTimes++;
    }
    }
    return rollTimes;
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: Help with creating a code about Java files

    The problem appears to be that you are using a single variable, rollTimes, and it is summing up all the throws, not just a particular one. I suggest you pass both the array and the number you are looking for as arguments. And do that for 1 thru 6 and print out the return value each time.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    incxx is offline Member
    Join Date
    Mar 2013
    Posts
    21
    Rep Power
    0

    Default Re: Help with creating a code about Java files

    Thank you, I was able to get my program to work :)

Similar Threads

  1. Creating Jar Files with functioning input files
    By appleLove in forum NetBeans
    Replies: 1
    Last Post: 04-10-2011, 10:37 PM
  2. Creating Files in Java?
    By Bagzli in forum New To Java
    Replies: 10
    Last Post: 03-02-2011, 01:56 AM
  3. Replies: 8
    Last Post: 07-30-2010, 05:47 PM
  4. Creating and running Java .ini files
    By Camzie in forum NetBeans
    Replies: 4
    Last Post: 11-25-2009, 12:42 PM
  5. Creating preview images of files in java
    By sandeepk84@gmail.com in forum Advanced Java
    Replies: 3
    Last Post: 07-04-2009, 10:29 PM

Posting Permissions

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