Categories

JAVA DATEBASE
Technology Network Community
Oracle Database
Fusion Middleware
Development Tools
Java
Desktop
Server & Storage Systems
Enterprise Management
Berkeley DB Family
Cloud Computing
Big Data
Business Intelligence
Architecture
Migration and Modernization
E-Business Suite
Siebel
PeopleSoft Enterprise
JD Edwards World
Industries
JD Edwards EnterpriseOne
User Productivity Kit Pro (UPK) and Tutor
Governance, Risk & Compliance (GRC)
Master Data Management (MDM)
Oracle CRM On Demand
On Demand: SaaS and Managed Applications
AutoVue Enterprise Visualization
Primavera
ATG
Agile PLM
Endeca Experience Management
Fusion Applications
Archived Forums

 



Tags

New To Java


counting vowels


I've been beating my brains trying to figure out why this program won't output the number vowels. Can someone point me in the right direction?  import java.util.Scanner;   public class CountVowels {   static Scanner console = new Scanner(System.in);   static String str; static int numberOfCharacters; static int vowelCount = 0; static int i; static char letter;   public static void main (String[] args)     {         System.out.print("Enter a string of characters: ");              str = console.next();              numberOfCharacters = str.length();                     for (int i = 0; i < numberOfCharacters; i++)     {                       if (isVowel(str.charAt(i)) == true)                       vowelCount++;     }       	System.out.println();     	System.out.println("Number of vowels = " + letter);         System.out.println();       }       public static boolean isVowel(char letter)     {         letter = str.charAt(i);             if (   letter == 'a' || letter == 'A' ||                    letter == 'e' || letter == 'E' ||                    letter == 'i' || letter == 'I' ||                    letter == 'o' || letter == 'O' ||                    letter == 'u' || letter == 'U'                    )                    return true;                    return false;   	}   } 

public static boolean isVowel(char letter)     {         //letter = str.charAt(i); DELETE THIS LINE             if (   letter == 'a' || letter == 'A' ||                    letter == 'e' || letter == 'E' ||                    letter == 'i' || letter == 'I' ||                    letter == 'o' || letter == 'O' ||                    letter == 'u' || letter == 'U'                    )                    return true;                    return false;   	} 

Also in terms of style  this  if (isVowel(str.charAt(i)) == true) vowelCount++;   would be better as  if (isVowel(str.charAt(i))) {   vowelCount++; } 

Same result with:  import java.util.Scanner;   public class CountVowels {   static Scanner console = new Scanner(System.in);   static String str; static int numberOfCharacters; static int vowelCount = 0; static int i; static char letter;   public static void main (String[] args)     {           System.out.print("Enter a string of characters: ");              str = console.next();              numberOfCharacters = str.length();                     for (int i = 0; i < numberOfCharacters; i++)     {                       if (isVowel(str.charAt(i)) == true)                       vowelCount++;       }       	System.out.println();     	System.out.println("Number of vowels = " + letter);         System.out.println();       }       public static boolean isVowel(char letter)     {         if (   letter == 'a' || letter == 'A' ||                    letter == 'e' || letter == 'E' ||                    letter == 'i' || letter == 'I' ||                    letter == 'o' || letter == 'O' ||                    letter == 'u' || letter == 'U'                    )                    return true;                    return false;   	}   } 

What is this supposed to do?  System.out.println("Number of vowels = " + letter); 

Ok I've changed it to  if (isVowel(str.charAt(i))) {   vowelCount++; } 

System.out.println("Number of vowels = " + letter);   Output the number of vowels in the string.

Same result with:  You have multiple problems but fixing that one was a start. See my last post.

System.out.println("Number of vowels = " + letter);   Output the number of vowels in the string.  Really now?  You want to explain where you output the number of vowels in the string?

Should I make another variable for the number of vowels?

Should I make another variable for the number of vowels?  You already have another variable for the number of vowels.

I changed   System.out.println("Number of vowels = " + letter);   to  System.out.println("Number of vowels = " + vowelCount);   It seems to work correctly now.

Hooray.

Thanks

One more question...  If I deleted:  letter = str.charAt(i)   Why did is it needed here:  if (isVowel(str.charAt(i)) == true)   Message was edited by:          mattvgt


Related Links

import problem
Inner Class vs IS - A relationship
Java libraries help
forward inside Filter and html frame target problem
programming help
calling methods on strings
Hiring someone to critique my code?
File Transfer: Sockets!!
java.lang.noclassdeffounderror:
ArrayList help
need help of the getInt method of the array class
Use on blur to change CSS style of a test field in a form
Quick question on strange path issue
about Sun Certified Programmer &#38; Developer
need a help in this dice program, having problem with looping
need info on NoSuchMethodError