Java 如何修改LogAnalyzer类的构造函数以包含调用LogReader类的参数?
首先,我知道几年前有人问过一个类似的问题,但出于某种原因,它似乎不起作用。代码片段是可以编译的,但是它不起作用,因为每次我尝试执行程序时,都会遇到这个错误 “java.lang.String无法转换为LogfileReader。” 我知道这个错误意味着什么,但我不知道为什么会发生这种情况,我没有足够的编码经验来独自解决它。此外,这是我第一次遇到必须使用“this”或构造函数内部的参数(而不是方法中的参数),因此我以前没有使用它的经验 我有一个理论,我需要包括某种Java 如何修改LogAnalyzer类的构造函数以包含调用LogReader类的参数?,java,bluej,Java,Bluej,首先,我知道几年前有人问过一个类似的问题,但出于某种原因,它似乎不起作用。代码片段是可以编译的,但是它不起作用,因为每次我尝试执行程序时,都会遇到这个错误 “java.lang.String无法转换为LogfileReader。” 我知道这个错误意味着什么,但我不知道为什么会发生这种情况,我没有足够的编码经验来独自解决它。此外,这是我第一次遇到必须使用“this”或构造函数内部的参数(而不是方法中的参数),因此我以前没有使用它的经验 我有一个理论,我需要包括某种LogfileReader=new
LogfileReader=newlogfilereader(r)代码>但这似乎也不正确
目标是创建一个LogfileAnalyzer对象,然后将我在LogfileCreator中创建的文件名传递给LogfileReader的构造函数。我相信这包括了“this”的一个实例,这本书暗示这可以在不修改LogfileReader类的情况下完成
这是LogfileAnalyzer中的相关代码位:
public class LogAnalyzer
{
// Where to calculate the hourly access counts.
private int[] hourCounts;
// Use a LogfileReader to access the data.
private LogfileReader reader;
/**
* Create an object to analyze hourly web accesses.
* Reads files created in LogfileCreator
*/
public LogAnalyzer(LogfileReader r)
{
this.reader = r;
hourCounts = new int[24];
}
这是基于之前回答的一个类似性质的问题
在使用LogfileCreator创建文件并在LogfileAnalyzer(LogfileReader)提示符中键入字符串(“This Blog”)之后,我最终遇到了上面的字符串错误
如果这有帮助,这是LogfileReader类的完整代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
/**
* A class to read information from a file of web server accesses.
* Currently, the log file is assumed to contain simply
* date and time information in the format:
*
* year month day hour minute
* Log entries are sorted into ascending order of date.
*
* @author David J. Barnes and Michael Kölling.
* @version 2016.02.29
*/
public class LogfileReader implements Iterator<LogEntry>
{
// The data format in the log file.
private String format;
// Where the file's contents are stored in the form
// of LogEntry objects.
private ArrayList<LogEntry> entries;
// An iterator over entries.
private Iterator<LogEntry> dataIterator;
/**
* Create a LogfileReader to supply data from a default file.
*/
public LogfileReader()
{
this("weblog.txt");
}
/**
* Create a LogfileReader that will supply data
* from a particular log file.
* @param filename The file of log data.
*/
public LogfileReader(String filename)
{
// The format for the data.
format = "Year Month(1-12) Day Hour Minute";
// Where to store the data.
entries = new ArrayList<>();
// Attempt to read the complete set of data from file.
boolean dataRead;
try{
// Locate the file with respect to the current environment.
URL fileURL = getClass().getClassLoader().getResource(filename);
if(fileURL == null) {
throw new FileNotFoundException(filename);
}
Scanner logfile = new Scanner(new File(fileURL.toURI()));
// Read the data lines until the end of file.
while(logfile.hasNextLine()) {
String logline = logfile.nextLine();
// Break up the line and add it to the list of entries.
LogEntry entry = new LogEntry(logline);
entries.add(entry);
}
logfile.close();
dataRead = true;
}
catch(FileNotFoundException | URISyntaxException e) {
System.out.println("Problem encountered: " + e);
dataRead = false;
}
// If we couldn't read the log file, use simulated data.
if(!dataRead) {
System.out.println("Failed to read the data file: " + filename);
System.out.println("Using simulated data instead.");
createSimulatedData(entries);
}
// Sort the entries into ascending order.
Collections.sort(entries);
reset();
}
/**
* Does the reader have more data to supply?
* @return true if there is more data available,
* false otherwise.
*/
public boolean hasNext()
{
return dataIterator.hasNext();
}
/**
* Analyze the next line from the log file and
* make it available via a LogEntry object.
*
* @return A LogEntry containing the data from the
* next log line.
*/
public LogEntry next()
{
return dataIterator.next();
}
/**
* Remove an entry.
* This operation is not permitted.
*/
public void remove()
{
System.err.println("It is not permitted to remove entries.");
}
/**
* @return A string explaining the format of the data
* in the log file.
*/
public String getFormat()
{
return format;
}
/**
* Set up a fresh iterator to provide access to the data.
* This allows a single file of data to be processed
* more than once.
*/
public void reset()
{
dataIterator = entries.iterator();
}
/**
* Print the data.
*/
public void printData()
{
for(LogEntry entry : entries) {
System.out.println(entry);
}
}
/**
* Provide a sample of simulated data.
* NB: To simplify the creation of this data, no
* days after the 28th of a month are ever generated.
* @param data Where to store the simulated LogEntry objects.
*/
private void createSimulatedData(ArrayList<LogEntry> data)
{
LogfileCreator creator = new LogfileCreator();
// How many simulated entries we want.
int numEntries = 100;
for(int i = 0; i < numEntries; i++) {
data.add(creator.createEntry());
}
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.net.URISyntaxException;
导入java.net.URL;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Iterator;
导入java.util.Random;
导入java.util.Scanner;
/**
*从web服务器访问文件中读取信息的类。
*目前,假定日志文件仅包含
*格式为的日期和时间信息:
*
*年月日时分
*日志条目按日期升序排序。
*
*@作者大卫·J·巴恩斯和迈克尔·科林。
*@version 2016.02.29
*/
公共类LogfileReader实现迭代器
{
//日志文件中的数据格式。
私有字符串格式;
//文件内容存储在表单中的位置
//日志条目对象的类型。
私有ArrayList条目;
//对条目的迭代器。
专用迭代器数据迭代器;
/**
*创建LogfileReader以从默认文件提供数据。
*/
公共日志文件读取器()
{
本文件(“weblog.txt”);
}
/**
*创建将提供数据的LogfileReader
*从特定的日志文件。
*@param filename日志数据文件。
*/
公共日志文件读取器(字符串文件名)
{
//数据的格式。
format=“年-月(1-12)日-小时-分钟”;
//在哪里存储数据。
entries=newarraylist();
//尝试从文件中读取完整的数据集。
布尔数据读取;
试一试{
//相对于当前环境定位文件。
URL文件URL=getClass().getClassLoader().getResource(文件名);
if(fileURL==null){
抛出新的FileNotFoundException(文件名);
}
扫描仪日志文件=新扫描仪(新文件(fileURL.toURI());
//读取数据行,直到文件结束。
while(logfile.hasNextLine()){
字符串logline=logfile.nextLine();
//将行拆分并添加到条目列表中。
日志条目=新日志条目(日志行);
条目。添加(条目);
}
logfile.close();
dataRead=true;
}
catch(FileNotFoundException | URISyntaxException e){
System.out.println(“遇到的问题:+e”);
dataRead=false;
}
//如果我们无法读取日志文件,请使用模拟数据。
如果(!dataRead){
System.out.println(“未能读取数据文件:“+filename”);
System.out.println(“改为使用模拟数据”);
创建模拟数据(条目);
}
//将条目按升序排序。
集合。排序(条目);
重置();
}
/**
*读者是否有更多的数据要提供?
*@return true如果有更多可用数据,
*否则就错了。
*/
公共布尔hasNext()
{
返回dataIterator.hasNext();
}
/**
*分析日志文件中的下一行并
*通过LogEntry对象使其可用。
*
*@返回一个日志条目,其中包含来自
*下一个日志行。
*/
公共日志条目下一步()
{
返回dataIterator.next();
}
/**
*删除条目。
*此操作是不允许的。
*/
公共空间删除()
{
System.err.println(“不允许删除条目”);
}
/**
*@返回解释数据格式的字符串
*在日志文件中。
*/
公共字符串getFormat()
{
返回格式;
}
/**
*设置一个新的迭代器以提供对数据的访问。
*这允许处理单个数据文件
*不止一次。
*/
公共无效重置()
{
dataIterator=entries.iterator();
}
/**
*打印数据。
*/
public void printData()
{
用于(日志条目:条目){
系统输出打印项次(输入);
}
}
/**
*提供模拟数据的样本。
*注意:为了简化此数据的创建,不需要
*一个月28日之后的天数将被生成。
*@param data用于存储模拟日志条目对象的位置。
*/
私有void createSimulatedData(ArrayList数据)
{
L