String text;
System.out.print ("Enter a String:");
text = console.nextLine();
int spaces = 0;
int consonants = 0;
int vowelcount = 0 ;
for (int index = 0; index < text.length(); index++) {
char letters = text.charAt(index);
if (letters == 'A' || letters == 'a')
vowelcount++;
else if (letters != 'a' && letters != 'e' && letters != 'i' && letters != 'o' && letters != 'u')
consonants++;
}
System.out.println ("Vowels:" + vowelcount + "\nConsonants :" + consonants + "\nSpaces : " + spaces);
示例输出 字符串:汉娜 输出的最后一部分 检测到的元音:a 检测到辅音:h n n h
答案 0 :(得分:4)
以下是一些帮助方法
public static boolean isVowel(char c){
String vowels = "aeiouAEIOU";
return vowels.contains(c);
}
public static boolean isConsanant(char c){
String cons = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";
return cons.contains(c);
}
在这里使用
char c = line.charAt(i);
int vowelCount = 0;
int consanantCount = 0;
int space = 0;
int punctuation = 0;
if (isVowel(c))
vowelCount++;
else if (isConsanant(c))
consanantCount++;
else if (Character.isWhitepace(c))
space++;
else
punctuation++;
答案 1 :(得分:0)
只需使用正则表达式,它只需要一行计数:
int spaces = text.replaceAll("\\S", "").length();
int consonants = text.replaceAll("(?i)[\\saeiou]", "").length();
int vowelcount = text.replaceAll("(?i)[^aeiou]", "").length();
这些都替换了匹配目标字符类型的字符而不是 - 有效删除它们 - 然后使用String.length()为您提供计数。
答案 2 :(得分:0)
您可以使用正则表达式:
我想下一个应该很容易;)
然后使用组匹配功能并计算所有实例
(我大多数时候在构建正则表达式时使用正则表达式工具,例如http://gskinner.com/RegExr/)
答案 3 :(得分:0)
要检测元音和辅音,您需要一个CONSONANTS字符数组,然后检查字符是否在此数组中。在这里你可以看到一个工作的例子,它计算辅音,元音和空格: import java.io.Console;
public class Vowels
{
public static final char[] CONSONANTS =
{
'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'
};
public static final char SPACE = ' ';
public static char[] getConsonants()
{
return CONSONANTS;
}
public static boolean isConsonant(char c)
{
boolean isConsonant = false;
for (int i = 0; i < getConsonants().length; i++)
{
if (getConsonants()[i] == c)
{
isConsonant = true;
break;
}
}
return isConsonant;
}
public static boolean isSpace(char c)
{
return SPACE == c;
}
public static void main(String[] args)
{
int spaces = 0;
int consonants = 0;
int vowelcount = 0;
Console console = System.console();
console.format("Enter a String:");
String text = console.readLine();;
for (int index = 0; index < text.length(); index++)
{
char letter = text.charAt(index);
if (!isSpace(letter))
{
if (isConsonant(letter))
{
consonants++;
}
else
{
vowelcount++;
}
}
else
{
spaces++;
}
}
System.out.println("Vowels:" + vowelcount + "\nConsonants :" + consonants + "\nSpaces : " + spaces);
}
}
答案 4 :(得分:0)
这是一种简单的方法,从How to count vowels and consonants
重新发布我的答案public static void checkVowels(String s){
System.out.println("Vowel Count: " + (s.length() - s.toLowerCase().replaceAll("a|e|i|o|u|", "").length()));
//Also eliminating spaces, if any for the consonant count
System.out.println("Consonant Count: " + (s.toLowerCase().replaceAll("a|e|i|o| |u", "").length()));
}
答案 5 :(得分:0)
使用LinkedHashSet
,因为它保留了顺序,并且不允许重复
//Check for vowel
public static boolean isVovel(char c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
return true;
}
return false;
}
public static void main(String[] args) {
String input = "shashi is a good boy";
char inter;
String vov = "";
String con = "";
String inp;
int len = input.length();
LinkedHashSet<String> vovels = new LinkedHashSet<String>();
LinkedHashSet<String> consonents = new LinkedHashSet<String>();
for (int i = 0; i < len; i++) {
inter = input.charAt(i);
inp = Character.toString(inter);
if (isVovel(inter)) {
vov = Character.toString(inter);
vovels.add(vov);
}
else {
con = Character.toString(inter);
consonents.add(con);
}
}
Iterator<String> it = consonents.iterator();
while (it.hasNext()) {
String value = it.next();
if (" ".equals(value)) {
it.remove();
}
}
System.out.println(vovels);
System.out.println(consonents);
}
答案 6 :(得分:0)
此示例是一个类,该类从文件中以字符串形式读取文本,将该文本存储为char数组,并初始化该数组的每个元素,从而将该元素转换为String并查看其是否与辅音正则表达式或元音匹配正则表达式。它会根据正则表达式匹配项来增加consonantCount或vowelCount,最后打印出计数。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;
public class CountVowlesAndConsonants {
public static void main (String [] args) throws IOException{
CountVowlesAndConsonants countVowlesAndConsonants = new CountVowlesAndConsonants();
countVowlesAndConsonants.countConsonatsAndVowles("/Users/johndoe/file.txt");
}
public void countConsonatsAndVowles(String file) throws IOException {
String text = readFile(file);
Pattern c = Pattern.compile("^(?![aeiouy]+)([a-z]+)$");
Pattern v = Pattern.compile("^[aeiouy]+$");
int vowelCount = 0;
int consonantCount = 0;
char [] textArray = text.toLowerCase().toCharArray();
for( char textArraz : textArray ){
String s = String.valueOf(textArraz);
if(c.matcher(s).matches()) {
consonantCount++;
} else if (v.matcher(s).matches()) {
vowelCount++;
}
}
System.out.println("VowelCount is " + vowelCount + " Constant Count " + consonantCount);
}
public String readFile(String file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader (file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
try {
while((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
return stringBuilder.toString();
} finally {
reader.close();
}
}
}
答案 7 :(得分:0)
我需要使用'loops for'来完成此功能,请遵循JavaScript中的示例:
#include