Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Program to count frequency of each digit in Pi  RSS feed

 
Kevin Sahakian
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone I'm new new here, and a beginner of java. I'm working on a school programming assignment in which I have to write a program to count the frequency of each of the digits 0 through 9 in the number pi. The full assignment includes reading from a supplied external file which holds the first 100,000 digits of pi, and reporting the frequency of digits using two classes. The file also has some "corruptions" such as random letters thrown in which I'll have to account for. However, I generally start these assignments by starting of simple and expanding on what I learn from how the code behaves.

So, for right now I simply have two classes and I'm supplying the first 30 or so digits of pi as a string and trying to count the frequencies of each digit. This is my code below:





I realize some of the code is unnecessary, but it's going to be part of the bigger picture which I haven't gotten to yet. The code compiles but I'm getting an "ArrayIndexOutOfBoundsException: -1" when I try to run the program. What I'm trying to do is make a tally array for each occurrence of a digit. Any thoughts on what I'm doing wrong, and help in general?
 
Kevin Sahakian
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, figured out this error was coming up because of the '.' in 3.14159...

updated code:





I'm getting these two errors:

  • Error: The method processLine(string) from the type PiCounter refers to the missing type string line: 18
  • Error: string cannot be resolved to a type line: 23


  • I'm not sure if I should treat the text file containing 100,000 digits of pi as reading in each individual line (which I don't know how to tell if it is broken up in to lines) or take the whole file at once and evaluate it, but I don't know how to tell my code to do that.
     
    Kevin Sahakian
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, last time I'm replying to my own topic here. I was able to get the answer myself, the correct code below.





    The only thing I feel I could have done better, is the way the program finds the "bad symbols" or non digits in the file. As it turns out, they are all white spaces besides the one decimal point at the beginning. Each time my code comes across a white space, it treats it as a new token and does a call to the processLine method. I'm able to account for all the white spaces by including charTally[10]++ to the readLines() method with the exception of the last white space (which is why I add 1 to charTally[10] at the end). I would have preferred that the scanner object read the whole text file at once, counting white spaces as it went.

    I'm going to leave it as it is for now, but if anyone has any comments or suggestions let me know.
     
    Stuie Clarky
    Ranch Hand
    Posts: 98
    Eclipse IDE Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You could extend your processLine method to check if ch is whitespace, then increment the appropriate counter if it is.
     
    Campbell Ritchie
    Marshal
    Posts: 55682
    162
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You ought not to use the numeric value method. Don’t use Exceptions instead of if‑else statements.
    You do realise you can do arithmetic with chars? Lots of people tell you that they are letters etc, but they are not. They are numbers.
    In that class, I think some of the fields should be moved to become local variables.
    Have you come across the nextLine() method of Scanner? Do you need to count the new line characters?
     
    Aj Prieto
    Ranch Hand
    Posts: 75
    Android Chrome Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Haha, I've had this assignment before.

    The way I got around the white space and . is to read in the file and put it into a character array.
    Then I would loop through the array calling Character.isDigit(char num), subtract 48 (to account for the ascii value) and increment the appropiate number.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!