Write a java program that repeatedly prompts the user to input a string starting with letters from the English alphabet. The program must stop getting input when the user inputs the string “STOOOOP”. Then the program must display the words starting with each letter from the alphabet in a separate line (e.g. you need to make a new line after all words starting with a specific letter are finished.
//Alphabet.java
import java.util.ArrayList;
import java.util.Scanner;
public class Alphabet {
public static void main(String[] args) {
ArrayList<String> words[] =
new ArrayList[26];
for (int i = 0; i < 26; i++)
{
words[i] = new
ArrayList<String>();
}
String s = "";
Scanner sc = new
Scanner(System.in);
while (!s.equals("STOOOOP"))
{
System.out.println("Enter a string (STOOOOP to stop): ");
s =
sc.next();
if
(s.equals("STOOOOP")) {
break;
} else {
if ((s.charAt(0) >= 'A' &&
s.charAt(0) <= 'Z') || (s.charAt(0) >= 'a' &&
s.charAt(0) <= 'z')) {
String temp =
s.toUpperCase();
int index = (int)
temp.charAt(0) - 65;
words[index].add(s);
}
}
}
for (int i = 0; i < 26; i++)
{
System.out.print((char) (i + 65) + ": ");
System.out.println(words[i].toString());
System.out.println();
}
}
}
//Code Snippet
Get Answers For Free
Most questions answered within 1 hours.