-2

I am trying to create a program in Java that reads from a file, extracts the first digit of every number, determines the frequencies of 0-9, and prints out the frequencies (in percentages) of the numbers 0 through 9. I already figured out how to read from my file ("lakes.txt");

    FileReader fr = new FileReader ("lakes.txt");        
    BufferedReader br = new BufferedReader(fr);

    //for loop that traverses each line of the file 
    int count = 0;
    for (String s = br.readLine(); s!= null; s = br.readLine()) {
        System.out.println(s); //print out every term
        count++;    
    }

    String [] nums;
    nums = new String[count];

    //close and reset file readers 
    fr.close();
    fr = new FileReader ("lakes.txt");        
    br = new BufferedReader(fr);

    //read each line of the file
    count = 0;

    for (String s = br.readLine(); s!= null; s = br.readLine()) {
        nums[count] = s;
        count++;
    }

I am currently printing out every term just to make sure it is working.

Now I am trying to figure out how to extract the first digit from each term in my string array.

For example, the first number in the array is 15,917, and I want to extract 1. The second number is 8,090 and I want to extract 8.

How can I do this?

JNYRanger
  • 6,829
  • 12
  • 53
  • 81
e21
  • 3
  • 1
  • You might consider using an array if int's to tally the frequency - each index (0-9) would represent a tally of each first digit. – copeg May 05 '15 at 00:02
  • possible duplicate of [What is the best way to get the first letter from a string in Java, returned as a string of length 1?](http://stackoverflow.com/questions/18201191/what-is-the-best-way-to-get-the-first-letter-from-a-string-in-java-returned-as) – Ben N May 05 '15 at 00:05

2 Answers2

0

To extract the first number from a String

  1. Get the first letter from the String
  2. Parse (1) into a number

For example:

String firstLetter = Character.toString(s.charAt(0));//alternatively use s.substring(0,1)
int value = Integer.parseInt(firstLetter);

This would be placed inside the file reading loop, assuming each line of the file contains a numeric value (in other words, no further processing or error handling of the lines of the file is required).

copeg
  • 8,290
  • 19
  • 28
0
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;

    public class TermReader {

        public static void main(String[] args) throws IOException {
            FileReader fr = new FileReader ("lakes.txt");        
            BufferedReader br = new BufferedReader(fr);

            int[] tally = new int[]{0,0,0,0,0,0,0,0,0,0};
            int total = 0;

            for (String s = br.readLine(); s!= null; s = br.readLine()) {
                char[] digits = s.toCharArray();
                for(char digit : digits) {
                    if( Character.isDigit(digit)) {
                        total++;
                        tally[Integer.parseInt(Character.toString(digit))]++;
                        break;
                    }
                }
            }       
            br.close();

            for(int index = 0; index < 10; index++) {
                double average = tally[index] == 0 ? 0.0 : (((double)tally[index]) / total) * 100;
                System.out.println("[" + index + "][" + tally[index] + "][" + total + "][" + Math.round(average * 100.0) / 100.0 + "]");
            }       
        }
    }
Constantin
  • 1,506
  • 10
  • 16