Advertisement
Musical_Muze

getBigWords method

Nov 8th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SentenceTool {
  4.  
  5.     public static void main(String[] args){
  6.        
  7.         Scanner scan = new Scanner(System.in);
  8.         System.out.println("Write a sentence, and I'll tell you how many words in that sentence contain more than five letters!");
  9.         String sentence = scan.nextLine();
  10.        
  11.         String[] output = SentenceTool.getBigWords(sentence);
  12.        
  13.         if(output.length == 0){
  14.             System.out.println("Your sentence did not contain any words of more than five letters.");
  15.         } else if (output.length == 1){
  16.             System.out.println(output[0] + " is the only word that was more than five letters from your sentence.");
  17.         } else {       
  18.             System.out.print("Your sentence contains " + output.length + " words that are five letters or longer: ");
  19.             for (int i=0; i<output.length; i++) {
  20.                 if(i != (output.length-1){
  21.                     System.out.print(output[i] + ", ");
  22.                 } else {
  23.                     System.out.print("and " + output[i] + ".");
  24.                 };
  25.             };
  26.        
  27.     };
  28.  
  29.     public static String[] getBigWords (String sentence) {
  30.        
  31.         int count = 0;
  32.         int numBigWords = 0;
  33.         String bigWord = "";
  34.        
  35.         for (int i=0; i<sentence.length(); i++){
  36.             if((sentence.charAt(i) >= 'a' && sentence.charAt(i) <= 'z') || (sentence.charAt(i) >= 'A' && sentence.charAt(i) <= 'Z')){
  37.                 count++;
  38.             } else {
  39.                 if(count>5){
  40.                     numBigWords++;
  41.                 };
  42.                 count = 0;
  43.             };
  44.             if (i == sentence.length()-1){
  45.                 if(count>5){
  46.                     numBigWords++;
  47.                 };
  48.             };
  49.         };
  50.        
  51.         String[] wordArray = new String[numBigWords];
  52.        
  53.         numBigWords = 0;
  54.         count = 0;
  55.        
  56.         for (int j=0; j<sentence.length(); j++){
  57.             if((sentence.charAt(j) >= 'a' && sentence.charAt(j) <= 'z') || (sentence.charAt(j) >= 'A' && sentence.charAt(j) <= 'Z')){
  58.                 count++;
  59.                 bigWord += sentence.charAt(j);
  60.             } else {
  61.                 if(count>5){
  62.                     wordArray[numBigWords] = bigWord;
  63.                     numBigWords++;
  64.                 };
  65.                 bigWord = "";
  66.                 count = 0;
  67.             };
  68.             if (j == sentence.length()-1){
  69.                 if(count>5){
  70.                     wordArray[numBigWords] = bigWord;
  71.                     numBigWords++;
  72.                 };
  73.             };
  74.         };
  75.        
  76.         return wordArray;
  77.        
  78.     };
  79. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement