Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: I just need help with this WHOLE program, O boy.

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I just need help with this WHOLE program, O boy.

    This programming assignment is about pi, you know: 3.14, and all that. You probably know someone who memorized some of its digits back in seventh grade. There are the 3.14 people, just about everyone, then there are the 3.14159, people, and finally there are the elite memorizers, who went to 3.14159265, and beyond. Pi is hard to memorize because its digits are seemingly random, and this assignment takes a first baby step toward assessing the randomness of number's digit pattern.(Click here for an accessible discussion of the nature of the digits of pi.)

    I've included a link to a text file that holds the first 100,000 digits of pi: digitsOfPt.txt
    Your job for this assignment is to read through that file and then build and display a table that reports the frequency of each of the digits 0 through 9. Your table should look like this:


    0 10001


    ..


    8 10271


    9 9994


    bad symbols: 123

    There are some other requirements.
    The file is actually slightly corrupted - it contains some symbols besides digits (spaces, for example). Your job as you read through the file is to skip over these, but also report how many non-digits there are in the file. To do this you can use Java's try-catch construction.
    Design arrays that most naturally use a non-numeric indexing scheme (here: the digits '0' through '9').
    Your solution must use inheritance.
    You must have a two class solution, a driver called PiDriver, and a principal class called PiCounter..

    SO the file is called digitsOfPt.txt
    and i need a PiDriver and a PiCounter
    if ANYONE could help with this i would APPRECIATE it a LOT.

    ALSO: it is due at 11:45pm EST tonight (July 31)
    Last edited by jwb4291; July 31st, 2010 at 02:39 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I just need help with this WHOLE program, O boy.

    Post what you have so far with the FULL text of the errors you are getting.
    If the program is executing but not giving what you want, show its output and show what you want it to do instead.

    To get your logic working, start with a small program with a String of digits and get the count to work with that first.

    Good luck on getting it working by tonight.

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I just need help with this WHOLE program, O boy.

    ya..i got nothing
    I have something that i think may work for the PiCounter but I cannot do drivers!
    can someone spot check my PiCounter and help with PiDriver?
    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
    public class PiCounter
    {
    public static void main(String[] args) throws IOException
    {
    Scanner inFile = new Scanner(new File("digitsOfPt.txt"));
    int[] counter = new int[11];
    String token = "";
     
    while(inFile.hasNext())
    {
    token += inFile.next();
    }
     
    for(int i = 0; i < token.length(); i++)
    {
    String character = token.substring(i, i + 1);
    if((int)character >= 48 && (int)character <= 57)
    {
    character = Integer.parseInt(character);
    counter[character]++;
    }
    else
    {
    counter[10]++;
    }
    }
     
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I just need help with this WHOLE program, O boy.

    What is the problem with the code you posted?

    You can use Arrays.toString(counter) to format an array for debugging output.

    What are the numbers in this code:
    if((int)character >= 48 && (int)character <= 57)

    Are you comparing against the characters '0' and '9'?
    The code would be much easier to understand if you used characters vs decimal values.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I just need help with this WHOLE program, O boy.


  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: I just need help with this WHOLE program, O boy.

    Two questions.

    1) isnt Pi just equal to 22/7? Dont think the question is very relevant to the problem, just wanted to confirm that.

    2) are you getting an index out of bounds error? Because you should be. You have set counter to have 11 spots. And in your if statement, you are referencing values based on the variable character. However in your if statement, you are checking if character is between 48 and 57. So basically, you are trying to reference array spots 48 through 57, which are WAY out of bounds. Also, the call (int)(String) cannot be done, which you are trying to do in your if statement. However, you can do Integer.parseInt(String), which you have also used in your if statement. There are two ways to cast ints, and I think you confused them. You can do Integer.parseInt(String), which will convert the String to an int. Or you can do (int)(double) to case the double as an int, by cutting off the decimals.

  7. #7
    Junior Member
    Join Date
    Jun 2010
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I just need help with this WHOLE program, O boy.

    Quote Originally Posted by aussiemcgr View Post
    Two questions.

    1) isnt Pi just equal to 22/7? Dont think the question is very relevant to the problem, just wanted to confirm that.

    no pi isnt equal to 22/7, that is just the closest fraction resembling pi. 22/7 = 3.14285714
    pi =3.14159265...etc