Question: I have many problem for my java code which can tell disaster. My teacher want to use at least two methods, one called numberVowels and
I have many problem for my java code which can tell disaster. My teacher want to use at least two methods, one called numberVowels and the other called stringCapitalize.
the ouput example should:
Enter the first string: java Enter the second string: class Class has 1 vowels. Java has 2 consonants.
My code is :
import java.util.Scanner;
public class HwkSample
{
public static int numberConsonants(String str)
{
int count=0;
for(int i=0;i { if(str.charAt(i) !='a' && str.charAt(i) !='e'&& str.charAt(i)!='i' && str.charAt(i)!='o' && str.charAt(i)!='u') count++; } return count; } public static int numberVowels(String str) { int count=0; for(int i=0;i { if (str.charAt(i)=='a'|| str.charAt(i)=='e'|| str.charAt(i)=='i'|| str.charAt(i)=='o'|| str.charAt(i)=='u') count++; } return count; } public static void int stringCapitalize(String str) { String s1=str1.substring(0,1).toUpperCase(); String c1=s1+str1.substring(1); String s2=str2.substring(0,1).toUpperCase(); String c2=s2+str2.substring(1); } public static void main(String args[]){ Scanner stdin =new Scanner(System.in); System.out.println("Enter the string1:"); String str1=stdin.nextLine(); System.out.println("Enter the string2:"); String str2=stdin.nextLine(); String str2 arr2=str2.toCharArray(); int consonants = NumberConsonants(s1); int vowels=NumberVowels(s2); if(Str1 < str2) { System.out.println(c1+" has "+vowels+" vowels."); System.out.println(c2+" has "+consonants+" consonants."); } else { System.out.println(c2+" has "+vowels+" vowels."); System.out.println(c1+" has "+consonants+" consonants."); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
