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 2 of 2

Thread: need help as soon as possible

  1. #1
    Banned
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help as soon as possible

    i've never programmed before, and the class i'm in now is confusing. i have to do the following:

    -read a text file given as a command-line argument
    -emit information about the data in the file:
    -the longest word (only letters count for length, earlier word wins ties)
    -number of vowels (aeiouy, upper & lower case)
    -number of consonants
    - number of punctuation characters (any non-letter non-whitespace)
    - number of words (bob is one word, don't is one word, bone-headed-assignment is one word)
    -number of chars (except whitespace)
    -how many words contain “ing”? (not “ING” or “iNg”) (“ringing” only counts as one)


    i've been working on this for hours, searching my books, notes, and the internet, but i keep finding a lot of info i don't understand. here's what i have so far:

      	import java.io.File;
        import java.io.FileNotFoundException;
        import java.util.Scanner;
     
     
        public class HW5 {
            public static void main(String[] args) {
                File stories = new File(args[0]);
                Scanner in = null;
                try {
                    in = new Scanner(stories);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    System.exit(0);
                }
                while (in.hasNext()) {
     
                   String sentence = in.nextLine().toLowerCase();
                   int numVowels = 0;
                   int numConsonants = 0;
                   for (int i=0; i<sentence.length(); i++)
                	   if (sentence.charAt(i) == 'a' || sentence.charAt(i) == 'e' || sentence.charAt(i) == 'i' || sentence.charAt(i) == 'o' || sentence.charAt(i) == 'u'|| sentence.charAt(i) == 'y')
                	   {
                           numVowels++;
                	   }
                       else if (Character.isLetter(sentence.charAt(i))) {
                           numConsonants++;
                       }
                   System.out.println("vowels: " + numVowels);
                   System.out.println("consonants: " + numConsonants);
     
     
                   int punct = 0;
                   for (int i=0; i<sentence.length(); i++)
                   if (!Character.isLetter(sentence.charAt(i)) && (!Character.isWhitespace(sentence.charAt(i)))) {
                	   punct++;
                   }
                   System.out.println("punct: " + punct);
     
     
                   int numberOfWords = 0;
                   for (int i=0; i<sentence.length(); i++)
                      if (Character.isLetter(sentence.charAt(i)) || (!Character.isWhitespace(sentence.charAt(i)) && (sentence.charAt(i) != '"') && (sentence.charAt(i) != ';') && (sentence.charAt(i) != '!') && (sentence.charAt(i) != '.') && (sentence.charAt(i) != ',') && (sentence.charAt(i) != '?') && (sentence.charAt(i) != ':')))
                      numberOfWords++; 
     
                      System.out.println("words: " + numberOfWords);
     
     
                   int charVal = 0;
                   for (int i=0; i<sentence.length(); i++)
                   if (Character.isLetter(sentence.charAt(i)) || (!Character.isWhitespace(sentence.charAt(i)))) {
                	   charVal++;
                   }
                   System.out.println("chars: " + charVal);
     
     
                   int ingVal = 0;
                   for (int i=0; i<sentence.length(); i++)
                	   if (Character.isLetter('i' + 'n' + 'g'))
                	   ingVal++;
     
     
                   System.out.println("ing: " + ingVal);
     
     
                   	}
                   }
                }

    i'm sure it's unnecessarily difficult and messy, but it's all i can do. i can't figure out how to count words properly - the sentence i'm using right now has 3 words, and this code shows 13 when i run it. i don't know how to handle the 'ing' thing. i have no idea how to figure out the longest word. i haven't learned about tokenizer or RegEx or whatever that's called, so most of the info on the internet means nothing to me.

    can anyone help? at least point me in the right direction? i'm so incredibly lost.

  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: need help as soon as possible

    You must see this: File Reading