1
/* txt file
Rolling Stone#Jann Wenner#Bi-Weekly#Boston#9000
Rolling Stone#Jann Wenner#Bi-Weekly#Philadelphia#8000
Rolling Stone#Jann Wenner#Bi-Weekly#London#10000
The Economist#John Micklethwait#Weekly#New York#42000
The Economist#John Micklethwait#Weekly#Washington#29000
Nature#Philip Campbell#Weekly#Pittsburg#4000
Nature#Philip Campbell#Weekly#Berlin#6000
*/   


 public class Zines {

            public static void main(String[] args) throws FileNotFoundException {
                Scanner input = new Scanner(new File("txt.file"));
                input.useDelimiter("#|\n|\r|\r\n");

                while(input.hasNext()) {  
                    String title = input.next();
                    String author = input.next();
                    String publisher = input.next();
                    String city = input.next();
                    String line = input.nextLine();
                    //int dist = Integer.valueOf(line);

                    System.out.println(line); 
        }
        }
    }

出力は次のとおりです。

"#9000
"#8000
"#10000
"#42000
"#29000
"#4000
"#6000  

出力 2:

9000
Rolling Stone
("Exception in thread "main") Jann Wenner
Weekly
Washington
4000
Nature
4

1 に答える 1

0

ここでの質問は、区切り文字を使用した後も # がまだ表示されるのはなぜですか?

Scanner#nextLine()最後の部分を読むために使用しているからです。区切り文字は考慮されません。次のトークンではなく、前に読み取ったトークンの後に残っているテキスト全体を読み取ります。

そのため、前に読み取ったトークンが である場合Boston、残りのテキスト -#9000は によって読み取られnextLine()ます。scanner#next()代わりに使用する必要があります。

String line = input.next();
于 2013-08-17T20:16:12.610 に答える