我試圖製作一個程序來反轉文件中的文本行。我仍然在學習java,而我對此很陌生。我的程序出錯了,因爲我在循環中創建了一個變量,並試圖在外部訪問它。我試圖在聲明字符串變量之前添加preffix「public」,但是當我嘗試編譯它時,它指向「public」並且表示非法開始表達式。有人可以告訴我爲什麼這是錯誤的,或者如何解決它。表達式字符串的非法開始
import java.io.*;
import java.util.*;
public class FileReverser
{
public static void main(String[] args)
throws FileNotFoundException
{
Scanner console = new Scanner(System.in);
System.out.print("File to Reverse: ");
String inputFileName = console.next();
System.out.print("Output File: ");
String outputFileName = console.next();
FileReader reader = new FileReader(inputFileName);
Scanner in = new Scanner(reader);
PrintWriter out = new PrintWriter(outputFileName);
int number = 0;
while (in.hasNextLine())
{
String line = in.nextLine();
public String[] lines;
lines[number] = line;
number++;
}
int subtract = 0;
for (int i;i>lines.length;i++)
{
out.println(lines[(lines.length-subtract)]);
subtract++;
}
out.close();
}
}
首先要修復:縮進。正確縮進代碼會使所有內容更容易理解。 –
人,請不要低估這個......他有一個真正的問題,如果有人絆倒在這,我們可以避免類似的問題。 –
@AnanthaSharma不錯的想法希望其他用戶像你一樣思考。 –