Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Homework 2-5 Write a Java program named Words that asks the user to enter four words. The program should then analyze each of the four

Homework 2-5

Write a Java program named Words that asks the user to enter four words.

The program should then analyze each of the four words so that it can report the length of each word as well as the positions of the five vowels within each word.

For a sample run where the user enters the words: she smiles third frisbee

the console screen must look as follows:

Enter 4 words: she smiles third frisbee Word 1 = "she" Length = 3 position of vowels: 'a' = -1, 'e' = 2, 'i' = -1, 'o' = -1, 'u' = -1

Word 2 = "smiles" Length = 6 position of vowels: 'a' = -1, 'e' = 4, 'i' = 2, 'o' = -1, 'u' = -1

Word 3 = "third" Length = 5 position of vowels: 'a' = -1, 'e' = -1, 'i' = 2, 'o' = -1, 'u' = -1

Word 4 = "frisbee" Length = 7 position of vowels: 'a' = -1, 'e' = 5, 'i' = 2, 'o' = -1, 'u' = -1

For a sample run where the user enters the words: shoe shoe chime wallabees

the console screen must look as follows:

Enter 4 words: shoe shoe chime wallabees Word 1 = "shoe" Length = 4 position of vowels: 'a' = -1, 'e' = 3, 'i' = -1, 'o' = 2, 'u' = -1

Word 2 = "shoe" Length = 4 position of vowels: 'a' = -1, 'e' = 3, 'i' = -1, 'o' = 2, 'u' = -1

Word 3 = "chime" Length = 5 position of vowels: 'a' = -1, 'e' = 4, 'i' = 2, 'o' = -1, 'u' = -1

Word 4 = "wallabees" Length = 9 position of vowels: 'a' = 1, 'e' = 6, 'i' = -1, 'o' = -1, 'u' = -1

Note:

For multiple instances of a vowel, only the index of the first occurrence will be indicated.

An index of -1 indicates that the vowel does not occur in the word.

Please make sure to end each line of output with a newline.

Please note that your class should be named Words.

Code I am using, but its not working.

import java.util.Scanner;

public class Words {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String[] words = new String[4];

System.out.println("Enter 4 words:");

for (int i = 0; i < 4; i++) {

words[i] = input.nextLine();

}

for (int i = 0; i < 4; i++) {

System.out.print("Word " + (i + 1) + "= \"" + words[i] + "\" Length = " + words[i].length() + " position of wovels: 'a' = ");

int a = words[i].indexOf("a");

System.out.print((a == -1) ? "-1" : a);

System.out.print(", 'e' = ");

int e = words[i].indexOf("e");

System.out.print((e == -1) ? "-1" : e);

System.out.print(", 'i' = ");

int iIndex = words[i].indexOf("i");

System.out.print((iIndex == -1) ? "-1" : iIndex);

System.out.print(", 'o' = ");

int o = words[i].indexOf("o");

System.out.print((o == -1) ? "-1" : o);

System.out.print(", 'u' = ");

int u = words[i].indexOf("u");

System.out.println((u == -1) ? "-1" : u);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions