我想在我的代码读取的 txt 的每一行中添加数字,但出现了这个错误:
LineNumbers.java:32:lineNum 已在 main(java.lang.String[]) 中定义
for(int lineNum = 0; lineNum <= line; lineNum++)
LineNumbers.java:32:运算符 <= 不能应用于 int,java.lang.String
for(int lineNum = 0; lineNum <= line; lineNum++)
我的代码是:
import java.util.Scanner;
import java.io.*;
public class LineNumbers
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
String fileName;
String line;
int lineNum = 0;
System.out.print("Enter file name: ");
fileName = keyboard.nextLine();
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
while (inputFile.hasNextLine())
{
line = inputFile.nextLine();
for(int lineNum = 0; lineNum <= line; lineNum++)
{
System.out.println(line);
}
}
inputFile.close();
}
}
任何帮助将不胜感激。谢谢你的时间!