我写了一个使用另一个类的flesch阅读程序。我的印象是,只需将两个类保存在同一个文件夹中就可以让一个人访问另一个但我收到错误。任何想法。
我得到的错误是:
Flesch.java:36: cannot find symbol
symbol : method getSyllableCt()
location: class Flesch
sllyablesCt = getSyllableCt();
肉体在这里:
public class Flesch{
public static void main(String args[])throws Exception{
int syllablesCt,
wordCt,
sentenceCt;
double flesch;
String listStr;
StringBuffer sb = new StringBuffer();
String inputFile = JOptionPane.showInputDialog("What file do you want to sort?");
BufferedReader inFile = new BufferedReader(new FileReader(inputFile));
sb.append(inFile.readLine());
//listStr = inFile.readLine();
while (inFile.readLine() != null){
sb.append(inFile.readLine());
//listStr = inFile.readLine();
}
Sentence sentence = new Sentence(sb);
wordCt = getWordCt();
sentenceCt = getSentenceCt();
System.out.println("The sentence count is" + sentenceCt);
System.out.println("The word count is" + wordCt());
Word word = new Word(getWords());
sllyablesCt = getSyllableCt();
System.out.println("The syllable count is" + syllablesCt);
flesch = (.39 * wordCt / sentenceCt) + (11.8 * syllablesCt / wordCt) - 15.59;
System.out.println("The Flesch Readability of this document is" + flesch);
inFile.close();
}
}
答案 0 :(得分:4)
如果方法存在于另一个类中,则它们需要(a)作为静态方法引用,或者(b)调用类的实例。
// Static method
int syllableCount = TheOtherClassName.getSyllableCt();
// Instance method
TheOtherClassName otherClass = new TheOtherClassName();
int syllableCount = otherClass.getSyllableCt();
然而,目前尚不清楚相关方法的存在位置,或者他们如何获取数据。
答案 1 :(得分:1)
如果方法在另一个类中,则需要将该类设为静态。
ClassName.getSyllableCt();
答案 2 :(得分:1)
sllyablesCt = getSyllableCt();
您的代码有拼写错误。那个变量不存在。将其更改为
syllablesCt = getSyllableCt();