gpt4 book ai didi

java - 用扫描仪回显文本文件并将其设为双倍行距?

转载 作者:行者123 更新时间:2023-11-30 06:25:14 25 4
gpt4 key购买 nike

我必须创建一个 EchoDouble 类来扩展 Echo 类,后者将文本文件回显到控制台。

这个文本文件的内容是

Nature's first green is gold, 
Her hardest hue to hold.
Her early leaf's a flower;
But only so an hour.

但我正在尝试编辑 EchoDouble 类中的 processLine 方法,以便它会像这样回显双倍行距的文本文件

Nature's first green is gold, 

Her hardest hue to hold.

Her early leaf's a flower;

But only so an hour.

回声等级

 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));
}

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

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

EchoDouble 类

import java.io.*;

public class EchoDouble extends Echo
{
public EchoDouble (String datafile) throws IOException
{
super(datafile);
}

// Prints the given line and inserts a blank line
// Overrides the processLine method in Echo class
public void processLine(String line)
{
/* **code here** */
}
}

我是 java 中的这个回显和扫描仪的新手,并且被困在这个问题上。如果有人可以就如何解决这个问题给我任何建议,我们将不胜感激!

最佳答案

这个怎么样:

@Override
public void processLine(String line){
System.out.println(line);
System.out.println();
}

关于java - 用扫描仪回显文本文件并将其设为双倍行距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15937627/

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