2013-10-31 228 views
-2

我有一個很快的問題。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「的名字在這裏」和「個人在這裏」只是爲了測試,如果代碼做了什麼,我預計它做的,這裏是存儲第一行作爲名字,第二行作爲個性等等。

我想讓這兩個字符串做另一件事。

如果我運行這段代碼,它將顯示錯誤「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:我是一個愚蠢的行嗎?不要嘲笑我。我完全是新手。謝謝 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!

+0

分配給'name'的是什麼? –

+0

你得到的輸出是什麼,你期望的輸出是什麼?你爲什麼認爲代碼在你的問題中正在做你說的話?你可以發佈一個完整的,可編輯的例子,顯示你遇到的問題? – vanza

+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 
0

當你讀到int(我假設你正在使用nextInt()此),撥打nextLine()之後;因爲nextInt()不讀取數字後面出現的新行字符。