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.