473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

prompts the user to input a sequence of characters and output the number of vowels

import java.util.*;
import java.lang.Character.*;

public class IsVowel
{

static char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };

public static boolean isVowel(char s)
{

boolean pass = false;
for(int i=0; i<6; i++)
if (s == vowels[i]) pass = true;
return pass;
System.out.print("Enter the number of vowels":);
System.out.println();
}
}
May 22 '07 #1
14 4655
r035198x
13,262 8TB
import java.util.*;
import java.lang.Character.*;

public class IsVowel
{

static char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };

public static boolean isVowel(char s)
{

boolean pass = false;
for(int i=0; i<6; i++)
if (s == vowels[i]) pass = true;
return pass;
System.out.print("Enter the number of vowels":);
System.out.println();
}
}
1.) Please use code tags everytime when posting code
2.) You do not have to import classes in the java.lang package.
3.) Write the main method which prompts for the input and take it using the Scanner class then call your isVowel method in that main method. You can post again if you get stuck with that.
May 23 '07 #2
The problem I see with your method is you confused a Boolean return for an int return. Your question said the method was supposed to output the number of vowels. This is what you should do. Declare an int type named count or something similar with same scope as the vowels array, initialize it with 0. change the name of the method to vowelNumber or something similar. Remove the Boolean variable, pass. You’re not checking for Booleans. Your decision statement should be: if the char variable s is found in the array of chars, vowels, then let it reflect in count. Try not to be careless, y is not a vowel. On a final note, after everything make sure your return is the last statement in your code. That’s the convention. Follow it.
I decided to write a code that reflects the above corrections

Expand|Select|Wrap|Line Numbers
  1. import java.util.Arrays;
  2.  
  3. public class VowelCount {
  4.  
  5.     //some variables were renamed to reflect your mistakes
  6.     static char[] chars = {'a','e','i','o','u'}; 
  7.     static int pass =0;
  8.  
  9.     private int vowelNumber(char s){
  10.         //if the char is a vowel, then it is a member of the array
  11.         if ((Integer)Arrays.binarySearch(chars, s)instanceof Integer)
  12.             ++pass;
  13.         return pass;    
  14.     }
  15.  
  16.     public static void main(String[] args) {
  17.         VowelCount vc = new VowelCount();
  18.         vc.vowelNumber('a');
  19.         vc.vowelNumber('e');
  20.         System.out.println(pass);
  21.  
  22.     }
  23.  
  24. }
May 25 '07 #3
JosAH
11,448 Expert 8TB
And here's a rather cryptic way that shows a glimpse of the power of regular expressions:
Expand|Select|Wrap|Line Numbers
  1.  
  2. public int nofVowels(String s) {
  3.    return s.replaceAll("[^aeiou]+","").length();
  4. }
kind regards,

Jos
May 25 '07 #4
r035198x
13,262 8TB
And here's a rather cryptic way that shows a glimpse of the power of regular expressions:
Expand|Select|Wrap|Line Numbers
  1.  
  2. public int nofVowels(String s) {
  3. return s.replaceAll("[^aeiou]+","").length();
  4. }
kind regards,

Jos
Yep, the power of regex and the size of Jos' bag of tricks.
May 25 '07 #5
When I realized you started with the wrong loop, I decided I had to do it myself, otherwise we could write a whole article on what you wanted. I hope you do understand the code.
Expand|Select|Wrap|Line Numbers
  1. package collection_framework;
  2.  
  3. /**a class which prints a number of
  4.  * (*) based on the power of two inputted 
  5.  * by the user through the main method
  6.  * @author nnaemeka david
  7.  */ 
  8.  
  9. public class PrintX {
  10.  
  11.     public void recurse(int n){
  12.         if (n>3) System.out.println("Only powers between one and three pls.");
  13.         deep: 
  14.             while (!(n <1)){
  15.                 if (n == 1){
  16.  
  17.                     //the following is taken as a constant.
  18.                     System.out.println("*");
  19.                     System.out.println("**");
  20.                     System.out.println("*");
  21.                     break deep;   //break stops looping forever. 
  22.  
  23.                 }else if (n ==2){
  24.                     recurse(1);      //call constant,
  25.                     System.out.println("****");    //print the needed power
  26.                     recurse(1);      //call constant
  27.                     break deep;      //stop looping forever
  28.  
  29.                 }else if (n==3){
  30.                     recurse(1);     //call constant
  31.                     System.out.println("****");   //call n-1th power
  32.                     recurse(1);     //constant
  33.                     System.out.println("********");  //nth power
  34.                     recurse(1);     //constant
  35.                     break deep;     //it's time to stop working!                        
  36.                 }
  37.             }
  38.     }
  39.  
  40.     public static void main(String[] args){
  41.         PrintX okay = new PrintX();
  42.         okay.recurse(3);
  43.     }
  44. }
  45.  
May 25 '07 #6
JosAH
11,448 Expert 8TB
Yep, the power of regex and the size of Jos' bag of tricks.
I *am* just a bag full of old tricks and I don't like typing code so all my code is
just terse and to the point ;-)

kind regards,

Jos
May 25 '07 #7
prometheuzz
197 Expert 100+
...

Expand|Select|Wrap|Line Numbers
  1. ...
  2.  
  3.     static char[] chars = {'a','e','i','o','u'}; 
  4.     static int pass =0;
  5.  
  6.     private int vowelNumber(char s){
  7.         //if the char is a vowel, then it is a member of the array
  8.         if ((Integer)Arrays.binarySearch(chars, s)instanceof Integer)
  9.             ++pass;
  10.         return pass;    
  11.     }
  12.  
  13.     ...
  14.  
  15. }
That method will always return pass+1, no matter what char gets passed to it as a parameter because Arrays.binarySearch(...) will always return an int (which you autobox to an Integer).
May 25 '07 #8
i don't think so! i tried it from 0.
May 25 '07 #9
prometheuzz
197 Expert 100+
i don't think so! i tried it from 0.
I think so!
What did you not understand about my explanation of the fact that Arrays.binarySearch(...) will always return an int, so your if statement: if((Integer)Arrays.binarySearch(chars, s)instanceof Integer), will always return true.

Here's your code again with an additional three lines in the main method:

Expand|Select|Wrap|Line Numbers
  1. import java.util.Arrays;
  2.  
  3. public class VowelCount {
  4.  
  5.     //some variables were renamed to reflect your mistakes
  6.     static char[] chars = {'a','e','i','o','u'};
  7.     static int pass =0;
  8.  
  9.     private int vowelNumber(char s){
  10.         //if the char is a vowel, then it is a member of the array
  11.         if ((Integer)Arrays.binarySearch(chars, s)instanceof Integer)
  12.             ++pass;
  13.         return pass;    
  14.     }
  15.  
  16.     public static void main(String[] args) {
  17.         VowelCount vc = new VowelCount();
  18.         vc.vowelNumber('a');
  19.         vc.vowelNumber('e');
  20.         vc.vowelNumber('1'); // added
  21.         vc.vowelNumber('%'); // added
  22.         vc.vowelNumber('z'); // added
  23.         System.out.println(pass);
  24.     }
  25. }
It prints out 5, which, needless to say, is wrong.
May 25 '07 #10
JosAH
11,448 Expert 8TB
I think so!
[ ... ]
It prints out 5, which, needless to say, is wrong.
But it is right in a Kaballah type of way: 5 is prime and "prime" contains 5
characters. "prime" also contains two vowels which is the correct answer to
your input. So there; :-P

kind regards,

Jos (runs -------------------------------------------------------------------------------> that a'way)
May 25 '07 #11
prometheuzz
197 Expert 100+
But it is right in a Kaballah type of way ...

(snip: higher number threory) ...
Isn't everything right in a Kaballah type of way? I mean, look at the Bible code:
http://en.wikipedia.org/wiki/Bible_code

Oh wait, you weren't serious...
; )
May 25 '07 #12
blazedaces
284 100+
But it is right in a Kaballah type of way: 5 is prime and "prime" contains 5
characters. "prime" also contains two vowels which is the correct answer to
your input. So there; :-P

kind regards,

Jos (runs -------------------------------------------------------------------------------> that a'way)
Just curious Jos, ata medaber ivrit? If you didn't understand that ignore it...

-blazed
May 26 '07 #13
prometheuzz
197 Expert 100+
Just curious Jos, ata medaber ivrit? If you didn't understand that ignore it...

-blazed
I don't think so, otherwise he would've spelled Kabbalah correctly.
; )
May 26 '07 #14
blazedaces
284 100+
I don't think so, otherwise he would've spelled Kabbalah correctly.
; )
Haha, well maybe he only knows how to spell it correctly in hebrew ;)

-blazed
May 26 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: s.chelliah | last post by:
We have an input field which allows the user to enter a string of (up to 62) ASCII printable characters. We have a need to allow the user to be able to specify any sequence of characters he/she...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
9
by: chuck | last post by:
I need some help with validating user input. I am writing a C computer program for an intro to C course. Here is the situation. I am creating an application that will do currency conversions. ...
0
by: Anonieko | last post by:
Are there any javascript codes there? Answer: Yes On the PageLoad event call InitialClientControsl as follows /// <summary> /// This will add client-side event handlers for most of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.