Question

In: Computer Science

Instructions: Create a Java program that reads a string entered by the user and then determines...

Instructions: Create a Java program that reads a string entered by the user and then determines and prints how many of each lowercase vowel (a, e, i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also, count and print the number of non-vowel characters.

Example: User enters: "This house is beautiful."

a: 1

e: 2

i: 3

o: 1

u: 2

non-vowel:10

Solutions

Expert Solution

The Solution:

import java.util.Scanner; //for accepting input
public class Main
{
   public static void main(String[] args) {
   //inntializing variables for count n input string
int aCount = 0, eCount = 0, iCount = 0;
int oCount = 0, uCount = 0, nonVowelCount = 0;
String userString;
  
//getting the string from user
Scanner sc = new Scanner (System.in);
System.out.println("Enter a string:");
userString = sc.nextLine();
  
for (int i = 0; i < userString.length(); i++) {
//reach each character of string and checking
char ch = userString.charAt(i);
  
if (ch== 'a') aCount++; //checking vowel 'a'   
else if (ch== 'e') eCount++; //checking vowel 'e'
else if (ch== 'i') iCount++; //checking vowel 'i'
else if (ch== 'o') oCount++; //checking vowel 'o'
else if (ch== 'u') uCount++; //checking vowel 'u'
//checking non-vowels
else if ((ch >= 'a'&& ch <= 'z')||(ch >= 'A'&& ch <= 'Z')) nonVowelCount++;   
}
  
//displaying the count of each vowel and non-vowels
System.out.println("a: " + aCount + "\n" +
"e: " + eCount + "\n" +
"i: " + iCount + "\n" +
"o: " + oCount + "\n" +
"u: " + uCount + "\n" +
"non-vowel:" + nonVowelCount);
}
}

The Screenshots:

The Code:

The Output:


Related Solutions

(JAVA) Write an application that reads three nonzero values entered by the user and determines and...
(JAVA) Write an application that reads three nonzero values entered by the user and determines and prints whether they could represent the sides of a triangle. Enter three sizes, separated by spaces(decimals values are acceptable): 4.5·5.5·3.5 A triangle could measure 4.50, 5.50, by 3.50.
in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
Write a Java program (name it PasswordTest) that reads from the user a string input (representing...
Write a Java program (name it PasswordTest) that reads from the user a string input (representing a password) and determines whether the password is “Valid Password” or “Invalid Password”. A valid password has at least 7 characters and includes at least one lower-case letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order to...
Write a java program using the following instructions: Write a program that determines election results. Create...
Write a java program using the following instructions: Write a program that determines election results. Create two parallel arrays to store the last names of five candidates in a local election and the votes received by each candidate. Prompt the user to input these values. The program should output each candidates name, the votes received by that candidate, the percentage of the total votes received by the candidate, and the total votes cast. Your program should also output the winner...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
using java language "Data Structures" I have to write program which stores string entered by user...
using java language "Data Structures" I have to write program which stores string entered by user into cursor array implementation here is the code public static void main(String[] args) { CursorArray sorted =new CursorArray();//the strings must added here how can i store them                  String []inputs = new String[50];                   for (int i=0; i< 50; i++) {            System.out.println("Enter the words you want to sort and use exit to stop");...
1) Create a "Can I be President?" program. The program determines if the user meets the...
1) Create a "Can I be President?" program. The program determines if the user meets the minimum requirements for becoming the President of the United States. Use user input. The rules for being president of the U.S. are: Older than 35 Resident of US for 14 Years Natural born citizen Print True if the person could be president and False if they can't be president. 2) Alter one line of that program to be a "I can't be President?" game....
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Using Java: Write a program that uses hash tables and reads in a string from the...
Using Java: Write a program that uses hash tables and reads in a string from the command line and a dictionary of words from standard input, and checks whether it is a "good" password. Here, assume "good" means that it (i) is at least 8 characters long, (ii) is not a word in the dictionary, (iii) is not a word in the dictionary followed by a digit 0-9 (e.g., hello5), (iv) is not two words separated by a digit (e.g.,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT