Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) throws FileNotFoundException {
- File f = new File(System.getProperty("user.dir") + "\\t.txt");
- Scanner sc = new Scanner(f);
- int line_ct = 0;
- int word_ct = 0;
- int char_ct = 0;
- int numb_ct = 0;
- ArrayList<String> total_words = new ArrayList<String>();
- ArrayList<String> total_nums = new ArrayList<String>();
- // while there are new tokens in the scanner
- while (sc.hasNext()) {
- String s_line = sc.nextLine(); // each line in String format
- line_ct++;
- while(s_line.isEmpty()) { // if the line is empty, move scanner next line
- s_line = sc.nextLine();
- }
- // line_ar[] is our LINE split on " ", line_ar[i] contains each word in a line
- String line_ar[] = s_line.split("\\s+");
- for(int i = 0; i < line_ar.length; i++) { //iterating through each words[]
- if(!line_ar[i].isEmpty()) {
- // put each word into a character array
- char[] chars = line_ar[i].toCharArray();
- //System.out.print(chars);
- if(Character.isLetter(chars[0])) { //if first char of chars[] is letter, increase word count
- word_ct++;
- total_words.add(line_ar[i].toLowerCase()); // append word to total_words
- }
- else if(Character.isDigit(chars[0])) { // if first char if chars[] is digit, increase num count
- numb_ct++;
- total_nums.add(line_ar[i]); // append number to total_nums
- }
- for(int b = 0; b < chars.length; b++) { // count each character in every word
- if(Character.isLetter(chars[b]) || Character.isDigit(chars[b])) { char_ct++; }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement