Results 1 to 4 of 4
  1. #1
    Goldie is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default Char Count Issue

    Hi all,

    I'm trying to count characters in a file that is read in and then added together. Here is my code:
    Java Code:
    import java.io.File;
        import java.io.FileNotFoundException;
        import java.util.Scanner;
    
        public class Stats {
            public static void main(String[] args) {
                File file = new File(args[0]);
                Scanner in = null;
                try {
                    in = new Scanner(file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    System.exit(0);
                }
                
                
                while (in.hasNext()) {
                    String word = in.next();
                    if (word == null)
                        return;
                    int counter = 0;
                    for (int i = 0; i < word.length(); i++) 
                    {
                        if (Character.isLetter(word.charAt(i))) counter++;
                       
                    }
                    System.out.println("Characters: " + counter);
    
                    }
                    
                    
                }
                }
    I'm using a txt file with the following in it:
    Where are you all going?

    Currently, the output I get is:
    Characters: 5
    Characters: 3
    Characters: 3
    Characters: 3
    Characters: 5

    I need it to look like this:
    Characters: 22

    I also will need to count how many vowels, consonants, ing, punctuation and words as well as what the longest word is. For example, the following output is what my program should eventually look like:
    Longest word: Where
    vowels: 10
    consonants: 9
    punct: 3
    words: 5
    chars: 22
    ing: 1

    So as you can see, my character count is already incorrect :(

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    int counter = 0;
    dont reset the counter each time, take it outside the loop and let it add up


    as for the rest of your duties, i suggest you read this:
    http://download.oracle.com/javase/tu...gex/intro.html
    Last edited by ozzyman; 03-27-2011 at 11:22 PM.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Quote Originally Posted by Goldie View Post
    Longest word: Where
    vowels: 10
    consonants: 9
    punct: 3
    words: 5
    chars: 22
    ing: 1
    There are 2 longest words: Where and going. The count for vowels and consonants is around the wrong way. I can only see 1 punctuation mark, which makes the char count wrong. WTF is ing?

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    i think he means words ending with "ing"; "ing words".

    you should use regex pattern matching to find all of this stuff:
    vowels: 10
    consonants: 9
    punct: 3
    ing: 1

    you can get a result instantly for the number of occurences

Similar Threads

  1. Replies: 2
    Last Post: 03-08-2011, 06:20 AM
  2. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 09:24 PM
  3. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  4. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 PM
  5. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 08:49 AM

Posting Permissions

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