Question #57918

Prompt the user to enter a three word phrase as one string and then:

Display the number of characters (including spaces) in the phrase.
Display the number of characters in the middle word.
Display the final word in all upper case.

Expert's answer

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a three word phrase as one string:");
String string;
String[] test;
do {
string = sc.nextLine();
test = string.trim().split("\\s+");
if(test.length != 3){
System.out.println("You enter not three word phrase!");
}
}while(test.length != 3);
System.out.println("The number of characters (including spaces) in the phrase: " + string.length());
System.out.println("The number of characters in the middle word: " + test[1].length());
System.out.println("The final word in all upper case: " + test[2].toUpperCase());
}
}
LATEST TUTORIALS
APPROVED BY CLIENTS