gpt4 book ai didi

java - 无法找出构造函数错误

转载 作者:行者123 更新时间:2023-12-02 15:27:24 27 4
gpt4 key购买 nike

<分区>

一段时间以来,我一直在努力完成以下作业。任务是创建一个程序,从输入文件中读取文本行,然后确定长度为 1 个字母、2 个字母等的单词的百分比。问题是我的类(class)扩展了另一个类(class),我正在运行进入构造函数问题,尽管我没有看到。

我扩展的类:

public abstract class FileAccessor{
String fileName;
Scanner scan;

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

public void processFile() {
while(scan.hasNext()){
processLine(scan.nextLine());
}
scan.close();
}

protected abstract void processLine(String line);

public void writeToFile(String data, String fileName) throws IOException{
PrintWriter pw = new PrintWriter(fileName);
pw.print(data);
pw.close();
}
}

我的类(class):

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

public class WordPercentages extends FileAccessor{
public WordPercentages(String s){
super.fileName = s;
super.scan = new Scanner(new FileReader(fileName));
}
public void processLine(String file){
super.fileName=file;
int totalWords = 0;
int[] length = new int[15];
scan = new Scanner(new FileReader(super.fileName));
while(super.scan.hasNext()){
totalWords+=1;
String s = scan.next();
if (s.length() < 15){
length[s.length()]+=1;
}
else if(s.length() >= 15){
length[15]+=1;
}
}
}

public double[] getWordPercentages(){
double[] percentages = new double[15];
for(int j = 1; j < percentages.length; j++){
percentages[j]+=length[j];
percentages[j]=(percentages[j]/totalWords)*100;
}
return percentages;
}
public double getAvgWordLength(){
double average;
for(int j = 1; j<percentages.length; j++){
average+=(j*percentages[j])/totalWords;
}
return average;
}
}

最后是我在运行我的驱动程序类时遇到的错误:

WordPercentages.java:8: error: constructor FileAccessor in class FileAccessor cannot be applied to given types;
public WordPercentages(String s) {
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com