Java无法检测文件内容

Java无法检测文件内容,java,newline,Java,Newline,我很难弄明白为什么这不起作用。Java只是没有执行while循环,文件显然没有下一行 fileName = getFileName(keyboard); file = new Scanner (new File (fileName)); pass = true; String currentLine; while (file.hasNextLine()) { currentLine = file.nextLine(); System.out.println(reverse(cur

我很难弄明白为什么这不起作用。Java只是没有执行while循环,文件显然没有下一行

fileName = getFileName(keyboard);
file = new Scanner (new File (fileName));
pass = true;
String currentLine;
while (file.hasNextLine()) {
    currentLine = file.nextLine();
    System.out.println(reverse(currentLine));
}
这是我正在测试的文件。我让它与前几段工作,但它似乎只是停止工作…:

贾伯沃基

那是一件漂亮的衣服 在瓦贝河中回旋和摇摆; 所有的米姆西都是波罗戈夫人, 而这一时刻却被超越了

“当心那些胡言乱语,我的儿子! 咬人的下颚,抓人的爪子! 小心朱布鸟,避开它 这是一个令人沮丧的班德斯纳奇!”

他手里拿着他的沃帕尔剑: 好久不见他所寻找的敌人了 他就这样在那棵树旁休息, 站在那里沉思了一会儿

就像乌菲什认为他站着一样, 有着火焰之眼的Jabberwock, 呼啸着穿过郁金香树林, 当它来的时候,它发出了刺耳的声音

一,二!一,二!彻底地 vorpal刀片变成了窃笑的零食! 他让它死了,还有它的头 他蹦蹦跳跳地回去了

“你杀了贾伯沃克吗? 到我的怀里来,我可爱的孩子! 哦,该死的一天!卡洛!卡洛!” 他高兴得哈哈大笑

那是一件漂亮的衣服 在瓦贝河中回旋和摇摆; 所有的米姆西都是波罗戈夫人, 而这一时刻却被超越了

--透过镜子,爱丽丝在那里发现了什么(1872年)


问题可能在于函数getFilename()或reverse()的实现中。既然你已经说过你用它来处理其中的几个段落,我怀疑你的程序是否因为你的文件处理而失败。可能是在您用来反转文件中导致问题的字符串的逻辑中。

您没有显示完整的异常堆栈跟踪。在catch块中,至少执行
e.printStackTrace()
。它从不执行catch块(或者至少在本例中不是)。上面的代码。这是你的
main()
方法吗?如果没有,你怎么知道它所属的方法真的被调用了?而不是这个
fileName=getFileName(键盘)尝试使用这个
fileName=keyBoard.nextLine().trim()
。您的
系统.out.println()
是否有打印出来的?如果他们这样做了,那么就会出现异常,因为while循环总是false,所以它实际上从未运行方法来反转代码。
/*
 * Lab13a.java
 * 
 *   A program that prompts the user for an input file name and, if that file exists,
 *   displays each line of that file in reverse order.
 *   Used to practice simple File I/O and breaking code up into methods as well as a first
 *   step to implementing Lab13b.java - reversing the entire file and Lab13c.java writing 
 *   output to a separate output file.
 * 
 * @author Benjamin Meyer
 * 
 */
package osu.cse1223;
import java.io.*;
import java.util.*;


public class Lab13a {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String fileName = "";
        Scanner file;
        boolean pass = false;
        while (!pass) {
            try {
                fileName = getFileName(keyboard);
                file = new Scanner (new File (fileName));
                pass = true;
                String currentLine;
                while (file.hasNextLine()) {
                    currentLine = file.nextLine();
                    System.out.println(reverse(currentLine));
                }
            }
            catch (FileNotFoundException e) {
                System.out.println("There was a problem reading from " + fileName);
                System.out.println("Goodbye.");
                return;
            }
        }

    }

    // Given a Scanner as input prompts the user to enter a file name.  If given an
    // empty line, respond with an error message until the user enters a non-empty line.
    // Return the string to the calling program.  Note that this method should NOT try
    // to determine whether the file name is an actual file - it should just get a
    // valid string from the user.
    private static String getFileName(Scanner inScanner) {
        boolean pass = true;
        String fileName = "";
        while (pass) {
            System.out.print("Enter an input name: ");
            fileName = inScanner.nextLine();
            if (fileName.length()!=0) {
                pass = false;
            }
            else {
                System.out.println("You cannot enter an empty string.");
            }
        }
        return fileName;
    }

    // Given a String as input return the reverse of that String to the calling program.
    private static String reverse(String inString) {
        if (inString.length()==0) {
            return "";
        }
        String reversed = "" + inString.charAt(inString.length()-1);
        for (int x = inString.length()-2; x>=0; x--) {
            reversed = reversed + inString.charAt(x);
        }
        return reversed;
    }


}