Welcome to Dream.In.Code
Become a Java Expert!

Join 150,042 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,609 people online right now. Registration is fast and FREE... Join Now!




Counting vowels

 
Reply to this topicStart new topic

Counting vowels

ibaraku
6 Jun, 2008 - 02:03 PM
Post #1

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 176



Thanked: 2 times
My Contributions
So I'm trying to write a program that reads the number of vowels and non_vowels of user entered sentence, I thought the code below would work, but it never enters the if statement that checks is it is a vowel or not, why is that, I thought it was allowed.
CODE

import java.util.*;

public class practice {
/*From book, page 170, problem 3.1*/

    public static void main(String[] args) {
        String sentence, check;
        int vowel_count = 0;
        int non_vowel_count = 0;
        int j = 1;
        System.out.println("Please enter a sentence: ");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        sentence = null;
        check = null;
        
        try{
            sentence = br.readLine();
        }catch(IOException ioe){
                System.out.println("Error reading sentence");
                System.exit(1);
        }
        
        sentence.toLowerCase();//Convert to lower case
        
        for (int i = 0; i <= sentence.length(); i++){
            
            if(j == (sentence.length()) + 1)
            {
                System.exit(2);
            }//exit when j is equal to null
            check = sentence.substring(i, j);
            j++;//increment j in order for substring to work
            
            if(check == "a" || check == "e" || check == "i" || check == "o" || check == "u")
            {
                ++vowel_count;
            }//end if
            else
            {
                ++non_vowel_count;
                
            }//end else
                      
        }//end for*/
        System.out.println("Vowel count is: "+vowel_count);
        System.out.println("Non vowel count is: "+non_vowel_count);
    }

}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Counting Vowels
6 Jun, 2008 - 02:17 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions

if(string1 == string2) is not a good idea
it will be true only if string1 and string2 points to same String in memory
not if the strings are equals

the real method to use is if(string1.equals(string2))

It is sure that the String that you extract from sentence does not point to the same memory location than "a", "e", ....

Here is your corrected code.

I used a Scanner lot easier
Convert sentence into an array or char and then check if equals to 'a', 'e', .....

java

import java.util.*;

public class practice {
/*From book, page 170, problem 3.1*/

public static void main(String[] args) {
int vowel_count = 0;
int non_vowel_count = 0;

Scanner scan = new Scanner(System.in);
System.out.print("Please enter a sentence: ");
String sentence = scan.nextLine();
sentence.toLowerCase();//Convert to lower case

char[] check = sentence.toCharArray();

for (int i = 0; i < sentence.length(); i++){
if(check[i] == 'a' || check[i] == 'e' || check[i] == 'i' || check[i] == 'o' || check[i] == 'u')
{
++vowel_count;
}//end if
else
{
++non_vowel_count;

}//end else

}//end for*/
System.out.println("Vowel count is: "+vowel_count);
System.out.println("Non vowel count is: "+non_vowel_count);
}

}


In French "y" is a voyel I guess it is not in English



User is online!Profile CardPM
+Quote Post

cutegrrl
RE: Counting Vowels
6 Jun, 2008 - 02:22 PM
Post #3

D.I.C Head
**

Joined: 12 May, 2008
Posts: 72



Thanked: 7 times
My Contributions
java

import java.util.Scanner;

public class CountVowels {

public static void main(String[] args) {

// Prompt user for input
Scanner in = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = in.nextLine().toLowerCase();

// Determine number of vowels
int numVowels = 0;
int numConsonants = 0;
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == 'a' || sentence.charAt(i) == 'e' || sentence.charAt(i) == 'i'
|| sentence.charAt(i) == 'o' || sentence.charAt(i) == 'u') {
numVowels++;
}
else if (Character.isLetter(sentence.charAt(i))) {
numConsonants++;
}
}

// Output results
System.out.println("Number of vowels: " + numVowels);
System.out.println("Number of consonants: " + numConsonants);

}
}

User is offlineProfile CardPM
+Quote Post

ibaraku
RE: Counting Vowels
6 Jun, 2008 - 02:37 PM
Post #4

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 176



Thanked: 2 times
My Contributions
Thanks for your help, it really helps!!!
User is offlineProfile CardPM
+Quote Post

skin_
RE: Counting Vowels
6 Jun, 2008 - 06:39 PM
Post #5

New D.I.C Head
*

Joined: 5 Apr, 2008
Posts: 33

Are you doing exercises from Lewis and Loftus? I did a bunch of those yesterday. Here's my modified version:

CODE
import java.util.Scanner;

public class vowelScan {

   public static void main(String[] args)
   {
           Scanner sc = new Scanner(System.in);
           int aCount, eCount, iCount, oCount, uCount;
           String userInput;
           
           aCount = 0;
           eCount = 0;
           iCount = 0;
           oCount = 0;
           uCount = 0;
           
           System.out.print("Input a string: ");
           userInput = sc.nextLine();
           
           for (int i = 0; i <= userInput.length()-1; i++)
           {
               if (userInput.charAt(i) == 'a')
               {
                   aCount++;
               }
               else if (userInput.charAt(i) == 'e')
               {
                   eCount++;
               }
               else if (userInput.charAt(i) == 'i')
               {
                   iCount++;
               }
               else if (userInput.charAt(i) == 'o')
               {
                   oCount++;
               }    
               else if (userInput.charAt(i) == 'u')
               {
                   uCount++;
               }
           }      
          
          System.out.println("There are: \n" + aCount + " 'a' characters. " + eCount + " 'e' characters. " + iCount + " 'i' characters. "
              + oCount + " 'o' characters. " + uCount + " 'u' charaters.");
   }
}

User is offlineProfile CardPM
+Quote Post

ibaraku
RE: Counting Vowels
6 Jun, 2008 - 11:26 PM
Post #6

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 176



Thanked: 2 times
My Contributions
QUOTE(skin_ @ 6 Jun, 2008 - 07:39 PM) *

Are you doing exercises from Lewis and Loftus? I did a bunch of those yesterday. Here's my modified version:

CODE
import java.util.Scanner;

public class vowelScan {

   public static void main(String[] args)
   {
           Scanner sc = new Scanner(System.in);
           int aCount, eCount, iCount, oCount, uCount;
           String userInput;
           
           aCount = 0;
           eCount = 0;
           iCount = 0;
           oCount = 0;
           uCount = 0;
           
           System.out.print("Input a string: ");
           userInput = sc.nextLine();
           
           for (int i = 0; i <= userInput.length()-1; i++)
           {
               if (userInput.charAt(i) == 'a')
               {
                   aCount++;
               }
               else if (userInput.charAt(i) == 'e')
               {
                   eCount++;
               }
               else if (userInput.charAt(i) == 'i')
               {
                   iCount++;
               }
               else if (userInput.charAt(i) == 'o')
               {
                   oCount++;
               }    
               else if (userInput.charAt(i) == 'u')
               {
                   uCount++;
               }
           }      
          
          System.out.println("There are: \n" + aCount + " 'a' characters. " + eCount + " 'e' characters. " + iCount + " 'i' characters. "
              + oCount + " 'o' characters. " + uCount + " 'u' charaters.");
   }
}



lol, yes I am, how do you like the book??? why do you think they created a Keyboard class instead of teaching how to really get user input???
User is offlineProfile CardPM
+Quote Post

mogstronaut_
RE: Counting Vowels
7 Jun, 2008 - 01:06 AM
Post #7

New D.I.C Head
*

Joined: 18 Apr, 2008
Posts: 47

A Keyboard class? What version do you have? I'm reading through 5th edition and it's great. Not really sure what you're talking about.

Also, there is an old Keyboard class in Java, but the Scanner class is used these days.
User is offlineProfile CardPM
+Quote Post

ibaraku
RE: Counting Vowels
7 Jun, 2008 - 09:14 AM
Post #8

D.I.C Head
Group Icon

Joined: 12 May, 2007
Posts: 176



Thanked: 2 times
My Contributions
QUOTE(mogstronaut_ @ 7 Jun, 2008 - 02:06 AM) *

A Keyboard class? What version do you have? I'm reading through 5th edition and it's great. Not really sure what you're talking about.

Also, there is an old Keyboard class in Java, but the Scanner class is used these days.


I am using the second edition, yes, very old I know =)
On chapter 2, (2.7) there is a Keyboard class section and they state:
"However, there is one very important characteristic of the Keyboard class to point out. The Keyboard class is not part of the Java standard class library. It has been written by the authors of this book..." (Page 83-84)
Now that you tell me that there used to be an old Keyboard class it makes a little bit more sense, but still, they state that they wrote it, and that the class is inside the cd that accompanies the book, so if you are very new to Java and you don't have the cd, you can't write a program that takes user input unless you do research somewhere else, at least that's what I had to do...
User is offlineProfile CardPM
+Quote Post

mensahero
RE: Counting Vowels
7 Jun, 2008 - 09:25 AM
Post #9

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
QUOTE(ibaraku @ 7 Jun, 2008 - 10:14 AM) *

QUOTE(mogstronaut_ @ 7 Jun, 2008 - 02:06 AM) *

A Keyboard class? What version do you have? I'm reading through 5th edition and it's great. Not really sure what you're talking about.

Also, there is an old Keyboard class in Java, but the Scanner class is used these days.


I am using the second edition, yes, very old I know =)
On chapter 2, (2.7) there is a Keyboard class section and they state:
"However, there is one very important characteristic of the Keyboard class to point out. The Keyboard class is not part of the Java standard class library. It has been written by the authors of this book..." (Page 83-84)
Now that you tell me that there used to be an old Keyboard class it makes a little bit more sense, but still, they state that they wrote it, and that the class is inside the cd that accompanies the book, so if you are very new to Java and you don't have the cd, you can't write a program that takes user input unless you do research somewhere else, at least that's what I had to do...


thats a very weird book.. Java is pack with many API.. why would the author reinvent something.. its just making simple things more complicated.. but I never really read that book.. I hardly even know it.. so probably theres a really deep reason why they do that..

well looking somewhere else is really cool.. if a simple task requires lots of code.. i'm better off searching somewhere for a better approach.. theres plenty of way to catch a fish.. blink.gif

User is offlineProfile CardPM
+Quote Post

mogstronaut_
RE: Counting Vowels
7 Jun, 2008 - 05:27 PM
Post #10

New D.I.C Head
*

Joined: 18 Apr, 2008
Posts: 47

Ah, that is pretty weird. You should read up on the Scanner class. It's really easy to use and better than using some custom class.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:55PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month