-2

I have a very quick question.

I have a file name "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

The first line is the name of the person, the second line is the personality of that person.

I want to capture the name, and personality into two different strings : name, getPersonal. But I don't know how. The way I write it, all the text will go to name. So how to fix it?

This is my code:

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);

}

EDIT 1 : the system.out.println "name is here", and "personal is here" is just for testing if the code did what I expected it to do, here is to store the first line as name, the second line as personality and so on.

I want these two string to do another things.

If I run this code, it will show an error "No Line Found". Since all the text will go to name, there is no text for personality.


EDIT 2: Here is the output :

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: ----------------------------------------------------------------------

Error:

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)

So how to fix it to take these lines into appropriate string?


EDIT 3: So I know why you guys see it works fine and mine is not. I forgot to put the "actual" first line.

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

Yes, it's an INT. So how can I skip that int ? and proceed to the next line?


EDIT 4 : I'M A STUPID OK? DON't LAUGH AT ME. I'M A TOTALLY NEWBIE. THANKS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

4

2 回答 2

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);

}

You're testing if the file has one next line, and if it has one next line, you're reading the two next lines.

It seems your file has an odd number of lines (the file ends with a line made of dashes, as the output shows), so the last iteration tries to read a line that doesn't exist.

EDIT:

To read a line and skip it: read it:

input.nextLine();
// now start looping to read the lines you really want to read
于 2013-10-31T22:02:24.557 回答
0

When you read that int (and I'm presuming you're using nextInt() for this), call nextLine() afterwards; because nextInt() does not read the new line character that occurs after the number.

于 2013-10-31T22:16:56.000 回答