对于Echo类型,未定义Java错误方法

时间:2014-04-14 01:30:42

标签: java error-handling

我在此代码的第11行收到错误" e.results();" 这是我的驱动程序类

import java.util.Scanner;
import java.io.*;
public class LineScrabbleDriver {
  public static void main(String args[]) throws IOException {
   String fileName;
   Scanner nameReader = new Scanner(System.in);
   System.out.println("Enter a file name");
   fileName = nameReader.nextLine();
   Echo e = new Echo(fileName);
   e.readLines();
   e.results();
  }
}

这是Echo的扩展类

import java.io.*;
public class LineScrabble extends Echo{
  double max = 0.0;
  String bestWord = "";
  int lineNumer = 0;
  public LineScrabble(String f) throws IOException {
    super(f);
  }
//scrabbles
int[] scrabbles = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
//process the given line
public void processLine(String s) {
  s.toLowerCase();
  int score = 0;
  for(int i = 0; i<s.length(); i++){
    char ch = s.charAt(i);
    if(Character.isLetter(ch)){
      int pos = ch - 'a';
      score += scrabbles[pos];
      }
    }
  if(score > max){
    max = score;
    bestWord = s;
  }
}
//displays the winner and score
public void results() {
System.out.println("Winner: " + bestWord);
System.out.println("score: " + max/bestWord.length());
}
}

这是Echo类

import java.util.Scanner;
import java.io.*;

  public class Echo{
    String fileName; // external file name
    Scanner scan; // Scanner object for reading from external file

    public Echo(String f) throws IOException
    {
     fileName = f;
     scan = new Scanner(new FileReader(fileName));
   }

   public void readLines(){ // reads lines, hands each to processLine
     while(scan.hasNext()){
       processLine(scan.nextLine());
     }
     scan.close();
  }

  public void processLine(String line){ // does the real processing work
     System.out.println(line);
   }
 }

我不太确定为什么它会给我一个未定义的类型错误。我是java编程的新手。请帮忙!

1 个答案:

答案 0 :(得分:0)

因为您使用的是原始Echo而不是LineScrabble,我认为您想要的是 -

// Echo e = new Echo(fileName); //
LineScrabble e = new LineScrabble(fileName);
e.readLines();
e.results(); // <-- Echo does not have results() you could add it there, or 
             // use LineScrabble.