Java,从文件中读取

时间:2013-10-31 21:45:44

标签: java

我的问题很快。

我有一个文件名“1234.txt”

Betty Boop
BABAAAABAAAAAAABAAAABBAAAAAABAAAABABAABAAABABABAABAAAAAABAAAAAABAAAAAA
Snoopy
AABBAABBBBBABABAAAAABABBAABBAAAABBBAAABAABAABABAAAABAABBBBAAABBAABABBB
Bugs Bunny
AABAABBABBBAAAABAAAABAAAAABABBBAABAAAABAABBBBABAAAABAABAAAAAABBAAAAABB
Daffy Duck
BAAAAA-BAAAABABAAAAAABA-AAAABABAAAABAABAA-BAAABAABAAAAAABA-BAAABA-BAAA
The frumious bandersnatch
-BBABAA-BBBBBABBBBA-BABBBBBBBBBABBBBBBABB-BBBABBABBBBBBB-BABBBBBBBBBBB
Minnie Mouse
BABA-AABABBBAABAABA-ABABAAAB-ABAAAAAA-AAAABAAABAAABAAAAAB-ABBAAAAAAAAA
Luke Skywalker
BBBAAABBBBAABA-BAAAABBABBAAABBAABAAB-AAAAABBBABAABABA-ABBBABBABAA-AAAA
Han Solo
BA-ABABBB-BBBAABABAAAABBAAABBAAABBABABBAAABABBAAABABAAAABBABAAABBABAAB
Princess Leia
BABBAAABBBBAAABBA-AAAABABBABBABBAAABAABAAABBBA-AABAABAAAABAAAAABABBBAA

第一行是该人的姓名,第二行是该人的个性。

我想将名称和个性捕获到两个不同的字符串中:name,getPersonal。 但我不知道怎么做。我编写它的方式,所有文本都将命名。那么如何解决呢?

这是我的代码:

while (input.hasNextLine()) {
            String name = input.nextLine();
            System.out.println("name is here: " + name);

            String getPersonal = input.nextLine();
            System.out.println(" personal is here " + getPersonal);

}

编辑1:system.out.println“名称在这里”,“personal is here”仅用于测试代码是否符合我的预期,这里是将第一行存储为名称,第二行作为人格等等。

我希望这两个字符串可以做另外的事情。

如果我运行此代码,它将显示错误“No Line Found”。由于所有文字都会出现名称,因此没有个性文字。


编辑2:这是输出:

Enter the name of the file with the personality data: 
1234.txt
name is here: 
 personal is here Betty Boop
name is here: BABAAAABAAAAAAABAAAABBAAAAAABAAAABABAABAAABABABAABAAAAAABAAAAAABAAAAAA
 personal is here Snoopy
name is here: AABBAABBBBBABABAAAAABABBAABBAAAABBBAAABAABAABABAAAABAABBBBAAABBAABABBB
 personal is here Bugs Bunny
name is here: AABAABBABBBAAAABAAAABAAAAABABBBAABAAAABAABBBBABAAAABAABAAAAAABBAAAAABB
 personal is here Daffy Duck
name is here: BAAAAA-BAAAABABAAAAAABA-AAAABABAAAABAABAA-BAAABAABAAAAAABA-BAAABA-BAAA
 personal is here The frumious bandersnatch
name is here: -BBABAA-BBBBBABBBBA-BABBBBBBBBBABBBBBBABB-BBBABBABBBBBBB-BABBBBBBBBBBB
 personal is here Minnie Mouse
name is here: BABA-AABABBBAABAABA-ABABAAAB-ABAAAAAA-AAAABAAABAAABAAAAAB-ABBAAAAAAAAA
 personal is here Luke Skywalker
name is here: BBBAAABBBBAABA-BAAAABBABBAAABBAABAAB-AAAAABBBABAABABA-ABBBABBABAA-AAAA
 personal is here Han Solo
name is here: BA-ABABBB-BBBAABABAAAABBAAABBAAABBABABBAAABABBAAABABAAAABBABAAABBABAAB
 personal is here Princess Leia
name is here: BABBAAABBBBAAABBA-AAAABABBABBABBAAABAABAAABBBA-AABAABAAAABAAAAABABBBAA
 personal is here Captain America
name is here: BBAA-BABAAABAABAABAAABBABABABAAA-ABBAAABAABAAABAABABA-AA-AAAAAABB-ABAA
 personal is here No Answer Dude
name is here: ----------------------------------------------------------------------

错误:

java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at Capitalize.getName(Capitalize.java:49)
at Capitalize.main(Capitalize.java:26)

那么如何修复它以将这些行带入适当的字符串?


编辑3:所以我知道为什么你们看到它运作正常而我的不是。我忘了把“实际”放在第一行。

11
Betty Boop
BABAAAABAAAAAAABAAAABBAAAAAABAAAABABAABAAABABABAABAAAAAABAAAAAABAAAAAA
Snoopy
AABBAABBBBBABABAAAAABABBAABBAAAABBBAAABAABAABABAAAABAABBBBAAABBAABABBB
Bugs Bunny
AABAABBABBBAAAABAAAABAAAAABABBBAABAAAABAABBBBABAAAABAABAAAAAABBAAAAABB
Daffy Duck
BAAAAA-BAAAABABAAAAAABA-AAAABABAAAABAABAA-BAAABAABAAAAAABA-BAAABA-BAAA
The frumious bandersnatch
-BBABAA-BBBBBABBBBA-BABBBBBBBBBABBBBBBABB-BBBABBABBBBBBB-BABBBBBBBBBBB
Minnie Mouse
BABA-AABABBBAABAABA-ABABAAAB-ABAAAAAA-AAAABAAABAAABAAAAAB-ABBAAAAAAAAA
Luke Skywalker
BBBAAABBBBAABA-BAAAABBABBAAABBAABAAB-AAAAABBBABAABABA-ABBBABBABAA-AAAA
Han Solo
BA-ABABBB-BBBAABABAAAABBAAABBAAABBABABBAAABABBAAABABAAAABBABAAABBABAAB
Princess Leia
BABBAAABBBBAAABBA-AAAABABBABBABBAAABAABAAABBBA-AABAABAAAABAAAAABABBBAA

是的,这是一个INT。那我怎么能跳过那个int呢?然后继续下一行?


编辑4:我是傻瓜好吗?不要笑我。我是一个完全新手。谢谢 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!

2 个答案:

答案 0 :(得分:1)

while (input.hasNextLine()) {
        String name = input.nextLine();
        System.out.println("name is here: " + name);

        String getPersonal = input.nextLine();
        System.out.println(" personal is here " + getPersonal);

}

您正在测试该文件是否有一个下一行,如果它有一个下一行,您正在阅读两个下一行。

看起来你的文件有一个奇数行(文件的末尾是一个由破折号组成的行,如输出所示),所以最后一次迭代尝试读取一条不存在的行。

编辑:

阅读一行并跳过它:阅读:

input.nextLine();
// now start looping to read the lines you really want to read

答案 1 :(得分:0)

当您读到int时(我假设您正在使用nextInt()),之后请致电nextLine();因为nextInt()不会读取数字后面出现的新行字符。