I need to use while loop.
error: bad operand type for binary operator ||.
first type: boolean
secod type: char
Assume that the letters A, E, I, O and U are vowels; any thing else is
consonant. Write a program that prompts the user to enter a string
(that consists only of letters - no numbers), and displays the number
of vowels and consonants in the string. Use a while loop.
Here is my code:
Scanner input = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = input.nextLine();
s = s.toUpperCase().trim();
int vowels = 0;
int consonants = 0;
int i = 0;
while (i < s.length()){
char ch = s.charAt(i);
if(ch == 'A' || ch == 'E' || ch == 'I'|| ch = 'O' || ch == 'U')
{
++vowels;
}
else {
consonants++;
}
i++;
System.out.println("The number of vowels is " + vowels);
System.out.println("The number of consonants is " + consonants);
}
}
}
I found another solution for my program. Below you can see the new program. This one is running. No problems.
Scanner input = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = input.nextLine();
int vowels = 0;
int consonants = 0;
int i = 0;
while (i < s.length()){
if (Character.isLetter(s.charAt(i))) {
if (Character.toUpperCase(s.charAt(i)) == 'A' ||
Character.toUpperCase(s.charAt(i)) == 'E' ||
Character.toUpperCase(s.charAt(i)) == 'I' ||
Character.toUpperCase(s.charAt(i)) == 'O' ||
Character.toUpperCase(s.charAt(i)) == 'U')
vowels++;
}else{
consonants++;
}
i++;
}
System.out.println("The number of vowels is " + vowels);
System.out.println("The number of consonants is " + consonants);
OUTPUT:
run:
Enter a sentence: We need to plan the next summer
The number of vowels is 9
The number of consonants is 6
BUILD SUCCESSFUL (total time: 1 minute 0 seconds)
Related
Create a 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.
When I created my program, the vowels return fine, but the consonant values are always off. Say the user inputs "I am feeling very cold today", the program returns the value of consonants as 19 when it should return it as 14.
Could you take a look at my program and help me determine why the returned consonant value is always wrong?
package CharacterCount;
import java.util.Scanner;
public class CharacterCount {
public static void main(String[] args) {
int a = 0, e = 0, x = 0;
int o = 0, u = 0, consonant = 0;
String str;
try (Scanner scan = new Scanner (System.in)) {
System.out.println("Enter a string");
str = scan.nextLine();
}
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c== 'a' || c== 'A') a++;
else if (c== 'e' || c== 'E') e++;
else if (c== 'i' || c== 'I') x++;
else if (c== 'o' || c== 'O') o++;
else if (c== 'u' || c== 'U') u++;
else consonant++;
}
System.out.println("a: " + a + "\n" +
"e: " + e + "\n" +
"i: " + x + "\n" +
"o: " + o + "\n" +
"u: " + u + "\n" +
"consonants:" + consonant);
}
}
looks like your program would currently consider a space character as a consonant... so you need to check for the space before incrementing your consonant variable
public class vowel {
public static void main(String args[])
{
String sentence;
int vowels = 0, digits = 0, blanks = 0, consonants=0;
char ch;
System.out.print("Enter a String : ");
sentence = TextIO.getln();
sentence = sentence.toLowerCase();
for(int i = 0; i < sentence.length(); i ++)
{
ch = sentence.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
vowels ++;
else if(ch =='b'|| ch == 'c' || ch == 'd'|| ch =='f' || ch =='g' ||
ch == 'h' || ch =='j' || ch =='k'|| ch =='l' || ch =='m' ||
ch == 'n' || ch =='p' || ch =='q'|| ch =='r' || ch =='s' ||
ch == 't' || ch =='v' || ch =='w'|| ch =='x' || ch =='z' ||
ch == 'y')
consonants ++;
else if(Character.isDigit(ch))
digits ++;
else if(Character.isWhitespace(ch))
blanks ++;
}
System.out.println("Vowels : " + vowels);
System.out.println("Consonants : " +consonants);
System.out.println("Digits : " + digits);
System.out.println("Blanks : " + blanks);
}
}
This program works perfectly in counting, but I wish to add on a function display the word it count
For example, input ABBCC12:
Vowels :1
Input Vowels : A
Consonants :4
Input Consonants : BBCC
Digits :2
Input Digits :12
Can I know what to do next?
Thanks in advance
It looks as though the easiest way that would fit with your current way of working would be to keep hold of a StringBuilder for each type:
vowelsStringBuilder = new StringBuilder();
and then whenever you encounter one, you add it on:
vowelsStringBuilder.append(ch);
At the end, you can then use
String vowelsString = vowelsStringBuilder.toString();
to get the final String containing all the vowels.
In fact, if you do it like this, you don't really need to count them as you go, because you can get the number of vowels at the end with vowelsString.length().
How I would get this code to count the vowels in each word rather than adding all the vowels up and displaying these. The code I have written:
import javax.swing.JOptionPane;
import java.util.Arrays;
String[] names = new String[12];
int i;
int j;
j=0;
int vowelCount;
vowelCount=0;
char ch;
names[0] = JOptionPane.showInputDialog("Please enter a name");
names[1] = JOptionPane.showInputDialog("Please enter a name");
names[2] = JOptionPane.showInputDialog("Please enter a name");
names[3] = JOptionPane.showInputDialog("Please enter a name");
names[4] = JOptionPane.showInputDialog("Please enter a name");
names[5] = JOptionPane.showInputDialog("Please enter a name");
names[6] = JOptionPane.showInputDialog("Please enter a name");
names[7] = JOptionPane.showInputDialog("Please enter a name");
names[8] = JOptionPane.showInputDialog("Please enter a name");
names[9] = JOptionPane.showInputDialog("Please enter a name");
names[10] = JOptionPane.showInputDialog("Please enter a name");
names[11] = JOptionPane.showInputDialog("Please enter a name");
Arrays.sort(names);
System.out.println("Name" + " " + "Characters" + " " + "Vowels");
for (i=0; i<12; i++)
{
for(j=0; j<names[i].length(); j++)
{
ch=names[i].charAt(j);
if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||
ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
vowelCount ++;
}
System.out.println(names[i] + " " +names[i].length() + " " + vowelCount);
}
I need the code to accept a user inputted name (which it does), sort the names alphabetically (which it does), count the characters in each name (which it does), and then display the vowels in each name.
Keep a collection of some sort, most likely an ArrayList. As you count the number of vowels, add them to the list.
//declare collection here
for(j=0; j<names[i].length(); j++)
{
ch=names[i].charAt(j);
if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||
ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
vowelCount ++;
//add ch character to collection
}
System.out.println(names[i] + " " +names[i].length() + " " + vowelCount);
//print vowels in collection
Example use of ArrayList
Just for extra information if you are interested, there are a number of collections you can use depending on whether you need a value/key pair, if you can allow duplicate entries, and if you want a certain order. Additionally, they perform differently depending on how you are using them.
Comparison Chart between different Collection
I was wondering as to how I could get the end of the program to repeat if the user does respond with a 1. Do I need to reorganize it so that it is part of the if statement?
Scanner input = new Scanner(System.in);
System.out.println("Count Vowels \n============");
System.out.println("Type a sentence and this program will tell you\n\nhow many vowels there are (excluding 'y'):");
String string1;
string1 = input.nextLine();
string1 = string1.toLowerCase();
int vowels = 0;
int answer;
int i = 0;
for (String Vowels : string1.split(" ")) {
for (i = 0; i < Vowels.length(); i++) {
int letter = Vowels.charAt(i);
if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u') {
vowels++;
}
}
System.out.println(Vowels.substring(0, 1).toUpperCase() + Vowels.substring(1) + " has " + vowels + " vowels");
vowels = 1;
}
System.out.println("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press 2");
answer = input.nextInt();
if (answer == 1) {
System.out.println("You have chosen to count the vowels in another phrase");
} else {
System.out.println("Have a nice day");
}
You can do this with a do/while loop. The skeleton for this kind of loop looks like this:
Scanner input = new Scanner(System.in);
do {
// do your stuff here
System.out.println("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press 2");
} while(input.nextInt() == 1);
System.out.println("Have a nice day");
It asks the user and evaluates the entered number in the while(input.nextInt() == 1) statement. If this comparison returns true (i.e. user entered 1), then the loops starts again. If not (i.e. user entered something else than 1), the loop stops and you'll get the "Good Bye" message instead.
you can split this up into more than one method and using one primary method call other methods inside a while loop. for example:
boolean continueCounting = false;
void countingVowels() {
//some start game method to make continueCounting = true
//something like "press 1 to start"
//if (input == 1) { continueCounting = true; }
while(continueCounting) {
String userInput = getUserInput();
countVowels(userInput); //count vowels in word from user input and prints them out to console
getPlayAgainDecision(); //ask user to put 1 or 2
if (answer == 1) {
continue
} else if (answer == 2) {
continueCounting = false;
} else {
System.out.println("incorrect input, please choose 1 or 2");
}
}
}
There are many ways to do this. A search on Google would have lead you to the correct answer in less time than it took you to ask the question. However, since you took the time to ask the question here is the answer:
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int answer = 0;
System.out.println("Count Vowels \n============");
// the do-while loop ensures that the code is executed at least once
do {
// on the first run answer equals zero, but on other runs it will equal one
if(answer == 1) {
System.out.println("You have chosen to count the vowels in another phrase");
}
System.out.println("Type a sentence and this program will tell you\n\nhow many vowels there are (excluding 'y'):");
String string1;
string1 = input.nextLine();
string1 = string1.toLowerCase();
int vowels = 0;
int i = 0;
for (String Vowels : string1.split(" ")) {
for (i = 0; i < Vowels.length(); i++) {
int letter = Vowels.charAt(i);
if (letter == 'a' || letter == 'e' || letter == 'i'
|| letter == 'o' || letter == 'u') {
vowels++;
}
}
System.out.println(Vowels.substring(0, 1).toUpperCase()
+ Vowels.substring(1) + " has " + vowels + " vowels");
vowels = 1;
}
System.out.println("Would you like to check another phrase in the Vowel Counter? If so type 1 if not type 2 and press enter");
answer = input.nextInt();
} while (answer == 1);
System.out.println("Have a nice day");
}
}
In your code you assert that a letter is a vowel if it is in the set a, e, i, o and u which is true. However, the letter y can be a vowel in certain situations.
In general, the Y is a consonant when the syllable already has a vowel. Also, the Y is considered a consonant when it is used in place of the soft J sound, such as in the name Yolanda or Yoda.
In the names Bryan and Wyatt, the Y is a vowel, because it provides the only vowel sound for the first syllable of both names. For both of these names, the letter A is part of the second syllable, and therefore does not influence the nature of the Y.
You could expand on your code even more by checking if the letter y is a vowel or not.
This is a more elegant way to do the counting (I updated the code to satisfy Johnny's comment that my previous answer didn't answer OP's question. The code now loops without unnecessary code):
public static void main(String... args)
{
int answer = 0;
Scanner input = null;
do
{
input = new Scanner(System.in);
System.out.print("Type a sentence and this program will tell you\nhow many vowels there are (excluding 'y'):");
String sentence = input.nextLine();
int vowels = 0;
String temp = sentence.toUpperCase();
for (int i = 0; i < sentence.length(); i++)
{
switch((char)temp.charAt(i))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowels++;
}
}
System.out.println("The sentence: \"" + sentence + "\" has " + vowels + " vowels");
System.out.print("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press any other key... ");
String tempNum = input.next();
try
{
answer = Integer.parseInt(tempNum);
} catch (NumberFormatException e)
{
answer = 0;
}
System.out.println();
} while (answer == 1);
input.close();
System.out.println("Have a nice day");
}
Notice that at the end, I catch a NumberFormatException for more robustness validation of the user's input.
Just put the main for loop inside a do-while loop, like so:
do
{
for (String Vowels : string1.split(" ")) {
for (i = 0; i < Vowels.length(); i++) {
int letter = Vowels.charAt(i);
if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u') {
vowels++;
}
}
System.out.println(Vowels.substring(0, 1).toUpperCase() +
Vowels.substring(1) + " has " + vowels + " vowels");
vowels = 1;
System.out.println("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press 2");
answer = input.nextInt();
}
} while (answer == 1);
System.out.println("Have a nice day");
Additionally, there are better ways to do the counting, for example:
for (char c : string1.toCharArray())
{
c = Character.toLowerCase(c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
count++;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
So I was given this assignment in class to make a counting vowel application. I have no idea what's wrong with my code, but please take a look.
package com.practice;
import java.util.*;
public class CountVowels {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int counter = 0;
int vowels = 0;
System.out.print("Enter text: ");
String text = input.nextLine();
int last = text.length() - 1;
while (last > 0) {
char temp = text.charAt(counter);
if (temp == 'a' || temp == 'e' || temp == 'i' || temp == 'o' || temp == 'u') {
vowels++;
}
counter++;
last++;
}
System.out.println("The number of vowels are: " + vowels);
}
}
Your program have infinity cycle and therefore calls IndexOfBoundException. If U want to loop cycle from end U need to decrement your last variable. Write --last; instead of last++ and change your cycle condition on while (last >= 0). To loop from start to end of your string change int last = text.length() - 1; on int last = 0; and your cycle condition on while (last < text.length()). You can choose a lot of different ways to solve this problem, but the main thing that you have understood the point.
You have to make your mistake correct as follows
Scanner input = new Scanner(System.in);
int vowels = 0;
System.out.print("Enter text: ");
String text = input.nextLine();
System.out.println(text);
int last = text.length() - 1;
while (last >= 0) {
char temp = text.charAt(last);
if (temp == 'a' || temp == 'e' || temp == 'i' || temp == 'o' || temp == 'u') {
vowels++;
}
last--;
// in your case this one ++ cause StringIndexOutOfBoundsException
}
System.out.println("The number of vowels are: " + vowels);
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int vowels = 0;
System.out.print("Enter text: ");
String text = input.nextLine();
int last = text.length() - 1;
for(int i=0;i<text.length();i++)
{
char temp = text.charAt(i);
if (temp == 'a' || temp == 'e' || temp == 'i' || temp == 'o' || temp == 'u') {
vowels++;
}
}
System.out.println("The number of vowels are: " + vowels);
}
The Output is:
Enter text: santhosh
The number of vowels are: 2