Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program t o read a sentence (as a string) from the user , the progr

ID: 3757878 • Letter: W

Question

Write a Java program to read a sentence (as a string) from the user, the program then prints out the entered sentence. Then, print out the sentence one word per line using the space (blank) character as are delimiter. Use proper labels for the outputs as shown in the examples below. Design your program such it allows the user to re-run the program with a different input.

Example Below--

Entered String: This is a test input, and can be a longer string too.

Word #1: This

Word #2: is

Word #3: a

Word #4: test

Word #5: input,

Word #6: and

Word #7: can

Word #8: be

Word #9: a

Word #10: longer

Word #11: string

Word #12: too.

Explanation / Answer

import java.util.Scanner;
class MyClass
{
public static void main(String args[]){

Scanner scanner = new Scanner(System.in);
       System.out.println("Enter a String:");
String str=scanner.nextLine();
String [] words=str.split(" "); // Split the words by space and strore it in words array.
        int i;
       for(i=0;i<words.length;i++)
        System.out.println("Word #"+(i+1)+": "+words[i]);
   }
}
          

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote