Seriously need help here
how can i wrte a program that read character only? i mean alphabert
like people key in "i +am+ 12567 a",;. boy,"
and the program only read iamaboy
is there another way other than lowercase the string and then compare a-z?
Seriously need help here
how can i wrte a program that read character only? i mean alphabert
like people key in "i +am+ 12567 a",;. boy,"
and the program only read iamaboy
is there another way other than lowercase the string and then compare a-z?
Programming isn't magic, every convievable method is not included in the java api. You need to write your own method for extracting alphabetic characters from a string. You can loop through the string, use tokenizers as said, use regular expressions to do something etc...
And what du you mean by write a program that reads alphabetic characters only? Where do you get your input from?
the question is actually like this
user enter a string
then write a program that count the consonant and vowel
i tried using comparison style but for some reason
when the string is short it show the correct answer(count correctly)
and when the string is lengthy
the answer is wrong
import java.util.Scanner;
public class Str {
//Declare a static scanner
static Scanner input = new Scanner(System.in);
//Data field
String Message;
//Default constructor
Str() {
}
//Constructor with argument
Str(String newMessage) {
Message = newMessage;
}
//Method int countVowels to count the vowels through comparison
int countVowels() {
//Counter
int count = 0;
//Convert the string to lower case to compare
String lowerMessage = Message.toLowerCase();
//Loop
for (int i = 0 ; i < lowerMessage.length() ; i++) {
//Declare x as character from string
char x = lowerMessage.charAt(i);
//Comparison
if (x =='a' || x =='e' || x =='i' || x == 'o' || x == 'u') {
count++;
}
}
//Return counter
return count;
}
//Method int countConsonants to count consonants through comparison method
int countConsonants() {
//Counter
int count = 0;
//Convert the string to lower case for comparison
String lowerMessage = Message.toLowerCase();
//Loop
for(int i = 0 ; i < lowerMessage.length() ; i++) {
//Declare x as character of string
char x = lowerMessage.charAt(i);
//Comparison
if ( x == 'q' || x == 'e' || x == 'r' || x == 't' || x == 'y' || x == 'p' || x =='s' || x == 'd'
|| x == 'f' || x == 'g' || x =='h' || x == 'j' || x =='k' || x == 'l' || x == 'z'
|| x == 'x' || x == 'c' || x == 'v' || x == 'b' || x == 'n' || x == 'm') {
count++;
}
}
//Return counter
return count;
}
}
You test for 'e' as a consonant. Did you mean 'w'?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.