2

I'm having trouble printing out the final result without each word being on its own line. The output should be formatted just as the input was. Here is the code I used to read the data and print it:

     Scanner sc2 = null;
        try {
            sc2 = new Scanner(new File(dataFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();  
        }
        while (sc2.hasNextLine()) {
                Scanner s2 = new Scanner(sc2.nextLine());
            boolean b;
            while (b = s2.hasNext()) {
                String s = s2.next();
                System.out.println(pig(s));
            }
        }

The actual instructions were as follows: "Translate the Declaration of Independence ("declaration.txt") into PigLatin. Try to preserve the paragraphs. There are several ways to do this, but they all use nested loops. You may want to look at nextLine, next, split, or StringTokenizer."

We haven't been taught how to use any of the methods listed there, though.

4

1 回答 1

0

println方法是“打印线”的简称。它将给定的输出打印到目标输出设备,后跟换行符。查看该类中的其他方法以获取解决方案。

更新

这里的问题是,据我所知java.util.Scanner,单词之间的空格(分隔符)会被丢弃。查看java.util.StringTokenizer可以配置为一次返回一个空白字符的类似类。

于 2012-09-08T20:52:22.093 回答