Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SentenceTool {
- public static void main(String[] args){
- Scanner scan = new Scanner(System.in);
- System.out.println("Write a sentence, and I'll tell you how many words in that sentence contain more than five letters!");
- String sentence = scan.nextLine();
- String[] output = SentenceTool.getBigWords(sentence);
- if(output.length == 0){
- System.out.println("Your sentence did not contain any words of more than five letters.");
- } else if (output.length == 1){
- System.out.println(output[0] + " is the only word that was more than five letters from your sentence.");
- } else {
- System.out.print("Your sentence contains " + output.length + " words that are five letters or longer: ");
- for (int i=0; i<output.length; i++) {
- if(i != (output.length-1){
- System.out.print(output[i] + ", ");
- } else {
- System.out.print("and " + output[i] + ".");
- };
- };
- };
- public static String[] getBigWords (String sentence) {
- int count = 0;
- int numBigWords = 0;
- String bigWord = "";
- for (int i=0; i<sentence.length(); i++){
- if((sentence.charAt(i) >= 'a' && sentence.charAt(i) <= 'z') || (sentence.charAt(i) >= 'A' && sentence.charAt(i) <= 'Z')){
- count++;
- } else {
- if(count>5){
- numBigWords++;
- };
- count = 0;
- };
- if (i == sentence.length()-1){
- if(count>5){
- numBigWords++;
- };
- };
- };
- String[] wordArray = new String[numBigWords];
- numBigWords = 0;
- count = 0;
- for (int j=0; j<sentence.length(); j++){
- if((sentence.charAt(j) >= 'a' && sentence.charAt(j) <= 'z') || (sentence.charAt(j) >= 'A' && sentence.charAt(j) <= 'Z')){
- count++;
- bigWord += sentence.charAt(j);
- } else {
- if(count>5){
- wordArray[numBigWords] = bigWord;
- numBigWords++;
- };
- bigWord = "";
- count = 0;
- };
- if (j == sentence.length()-1){
- if(count>5){
- wordArray[numBigWords] = bigWord;
- numBigWords++;
- };
- };
- };
- return wordArray;
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement