-3

I need to manipulate this code so that it will read the # of digits from a file. I am honestly stumped on this one for some reason. Do i need to tokenize it first? Thanks!

import java.io.*;
import java.util.*;

public class CountLetters {

    public static void main(String args[]) {
        if (args.length != 1) {
            System.err.println("Synopsis: Java CountLetters inputFileName");
            System.exit(1);
        }
        String line = null;
        int numCount = 0;
        try {
            FileReader f = new FileReader(args[0]);
            BufferedReader in = new BufferedReader(f);
            while ((line = in.readLine()) != null) {
                for (int k = 0; k < line.length(); ++k)
                    if (line.charAt(k) >= 0 && line.charAt(k) <= 9)
                       ++numCount;
            }
            in.close();
            f.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(numCount + " numbers in this file.");
    } // main
} // CountNumbers
0ddlyoko
  • 310
  • 4
  • 14
Kitty
  • 1
  • 2
    You should [make sure your code is indented properly](https://stackoverflow.com/posts/46385103/edit) if you are asking people to try and read it. – khelwood Sep 23 '17 at 23:18
  • Welcome to Stack Overflow. What have you already tried yourself to do this? Please review [How do I ask a good question](https://stackoverflow.com/help/how-to-ask). Stack Overflow is not a coding service. You are expected to ***research your issue and make a good attempt to write the code yourself*** before posting. If you get stuck on something *specific*, come back and include a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and a summary of what you tried, so we can help. – FluffyKitten Sep 24 '17 at 01:51
  • what is your input file? – tommybee Sep 24 '17 at 04:48

2 Answers2

2

Use '' to indicate a char constant (you are comparing chars to ints), also I would suggest you use try-with-resources Statement to avoid explicit close calls and please avoid using one line loops without braces (unless you are using lambdas). Like

public static void main(String args[]) {
    if (args.length != 1) {
        System.err.println("Synopsis: Java CountLetters inputFileName");
        System.exit(1);
    }
    String line = null;
    int numCount = 0;
    try (BufferedReader in = new BufferedReader(new FileReader(args[0]))) {
        while ((line = in.readLine()) != null) {
            for (int k = 0; k < line.length(); ++k) {
                if ((line.charAt(k) >= '0' && line.charAt(k) <= '9')) {
                    ++numCount;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(numCount + " numbers in this file.");
} // main

Also, you could use a regular expression to remove all non-digits (\\D) and add the length of the resulting String (which is all-digits). Like,

while ((line = in.readLine()) != null) {
    numCount += line.replaceAll("\\D", "").length();
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

Use if(Charachter.isDigit(char)) replace char with each char, this will count each number, and I believe arabic numbers as well.

AnthonyK
  • 473
  • 2
  • 11