Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. public static void main(String[] args) throws FileNotFoundException {
  2.  
  3. File f = new File(System.getProperty("user.dir") + "\\t.txt");
  4. Scanner sc = new Scanner(f);
  5.  
  6. int line_ct = 0;
  7. int word_ct = 0;
  8. int char_ct = 0;
  9. int numb_ct = 0;
  10.  
  11. ArrayList<String> total_words = new ArrayList<String>();
  12. ArrayList<String> total_nums = new ArrayList<String>();
  13.  
  14. // while there are new tokens in the scanner
  15. while (sc.hasNext()) {
  16. String s_line = sc.nextLine(); // each line in String format
  17. line_ct++;
  18. while(s_line.isEmpty()) { // if the line is empty, move scanner next line
  19. s_line = sc.nextLine();
  20. }
  21. // line_ar[] is our LINE split on " ", line_ar[i] contains each word in a line
  22. String line_ar[] = s_line.split("\\s+");
  23.  
  24. for(int i = 0; i < line_ar.length; i++) { //iterating through each words[]
  25.  
  26. if(!line_ar[i].isEmpty()) {
  27. // put each word into a character array
  28. char[] chars = line_ar[i].toCharArray();
  29.  
  30. //System.out.print(chars);
  31.  
  32. if(Character.isLetter(chars[0])) { //if first char of chars[] is letter, increase word count
  33. word_ct++;
  34. total_words.add(line_ar[i].toLowerCase()); // append word to total_words
  35. }
  36. else if(Character.isDigit(chars[0])) { // if first char if chars[] is digit, increase num count
  37. numb_ct++;
  38. total_nums.add(line_ar[i]); // append number to total_nums
  39. }
  40.  
  41. for(int b = 0; b < chars.length; b++) { // count each character in every word
  42. if(Character.isLetter(chars[b]) || Character.isDigit(chars[b])) { char_ct++; }
  43. }
  44.  
  45. }
  46.  
  47. }
  48.  
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement