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();
}
}
14 4655
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.
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 - import java.util.Arrays;
-
-
public class VowelCount {
-
-
//some variables were renamed to reflect your mistakes
-
static char[] chars = {'a','e','i','o','u'};
-
static int pass =0;
-
-
private int vowelNumber(char s){
-
//if the char is a vowel, then it is a member of the array
-
if ((Integer)Arrays.binarySearch(chars, s)instanceof Integer)
-
++pass;
-
return pass;
-
}
-
-
public static void main(String[] args) {
-
VowelCount vc = new VowelCount();
-
vc.vowelNumber('a');
-
vc.vowelNumber('e');
-
System.out.println(pass);
-
-
}
-
-
}
And here's a rather cryptic way that shows a glimpse of the power of regular expressions: -
-
public int nofVowels(String s) {
-
return s.replaceAll("[^aeiou]+","").length();
-
}
kind regards,
Jos
And here's a rather cryptic way that shows a glimpse of the power of regular expressions: -
-
public int nofVowels(String s) {
-
return s.replaceAll("[^aeiou]+","").length();
-
}
kind regards,
Jos
Yep, the power of regex and the size of Jos' bag of tricks.
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. - package collection_framework;
-
-
/**a class which prints a number of
-
* (*) based on the power of two inputted
-
* by the user through the main method
-
* @author nnaemeka david
-
*/
-
-
public class PrintX {
-
-
public void recurse(int n){
-
if (n>3) System.out.println("Only powers between one and three pls.");
-
deep:
-
while (!(n <1)){
-
if (n == 1){
-
-
//the following is taken as a constant.
-
System.out.println("*");
-
System.out.println("**");
-
System.out.println("*");
-
break deep; //break stops looping forever.
-
-
}else if (n ==2){
-
recurse(1); //call constant,
-
System.out.println("****"); //print the needed power
-
recurse(1); //call constant
-
break deep; //stop looping forever
-
-
}else if (n==3){
-
recurse(1); //call constant
-
System.out.println("****"); //call n-1th power
-
recurse(1); //constant
-
System.out.println("********"); //nth power
-
recurse(1); //constant
-
break deep; //it's time to stop working!
-
}
-
}
-
}
-
-
public static void main(String[] args){
-
PrintX okay = new PrintX();
-
okay.recurse(3);
-
}
-
}
-
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
... - ...
-
-
static char[] chars = {'a','e','i','o','u'};
-
static int pass =0;
-
-
private int vowelNumber(char s){
-
//if the char is a vowel, then it is a member of the array
-
if ((Integer)Arrays.binarySearch(chars, s)instanceof Integer)
-
++pass;
-
return pass;
-
}
-
-
...
-
-
}
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).
i don't think so! i tried it from 0.
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: - import java.util.Arrays;
-
-
public class VowelCount {
-
-
//some variables were renamed to reflect your mistakes
-
static char[] chars = {'a','e','i','o','u'};
-
static int pass =0;
-
-
private int vowelNumber(char s){
-
//if the char is a vowel, then it is a member of the array
-
if ((Integer)Arrays.binarySearch(chars, s)instanceof Integer)
-
++pass;
-
return pass;
-
}
-
-
public static void main(String[] args) {
-
VowelCount vc = new VowelCount();
-
vc.vowelNumber('a');
-
vc.vowelNumber('e');
-
vc.vowelNumber('1'); // added
-
vc.vowelNumber('%'); // added
-
vc.vowelNumber('z'); // added
-
System.out.println(pass);
-
}
-
}
It prints out 5, which, needless to say, is wrong.
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)
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...
; )
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
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.
; )
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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. ...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
| |