【发布时间】:2016-04-05 00:46:07
【问题描述】:
我无法完成这段关于计算字符串中元音和辅音数量的代码。 每当我将字符串发送到类时,getVowelCount 方法和 getConstCount 方法都会出现错误,我相信问题出在我尝试使用字符串的特定字符调用 isLetter 方法的两个方法的循环但我不确定。我明白了
线程“main”中的异常 java.lang.NullPointerException / 在 DCMvowelsAndConsonants.getVowelCount(DCMvowelsAndConsonants.java:30) / 在 DCMvowelsAndConsonantsDriver.main(DCMvowelsAndConsonantsDriver.java:40)
我不确定是否应该发布驱动程序,因为这已经很大了。
public class DCMvowelsAndConsonants
{
String line;
public DCMvowelsAndConsonants()
{
String line = " ";
}
public DCMvowelsAndConsonants(String l)
{
String line = l;
}
public static boolean isLetter(char i)
{
return i == 'a' || i == 'A' || i == 'e' || i == 'E' || i == 'i' || i == 'I' || i == 'o' || i == 'O' || i == 'u' || i == 'U';
}
public int getVowelCount()
{
int vowelCount = 0;
for(int i = 0; i < line.length(); i++)
{
if(isLetter(line.charAt(i)))
vowelCount++;
}
return vowelCount;
}
public int getConstCount()
{
int constCount = 0;
for(int i = 0; i < line.length(); i++)
{
if(!isLetter(line.charAt(i)))
constCount++;
}
return constCount;
}
}
【问题讨论】:
-
@TimBiegeleisen “我看不出你的代码有问题。” 然后重新检查他的构造函数
-
@Tom Facepalm。随时发布您的答案。
-
标签: java