简体   繁体   中英

StringTokenizer not making tokens with new lines

I am having trouble making a StringTokenizer break up a few lines of text into separate tokens. The inputted text is

3
monkeys

But, the StringTokenizer is apparently interpreting this as "3monkeys". I need it to be "3" and "monkeys". Because of this, a NumberFormatException is being throw since I need to convert the string "3" into an integer.

This is the code I have so far.

    Scanner f = new Scanner( new FileInputStream("input.txt" )); // Yes, i have the actual input file path here, but I changed it for this question.
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));

    String temp = "";
    //String cString = "";

    while( f.hasNextLine() ) {
        String cString = f.nextLine();
        temp += cString;
    }
    StringTokenizer everythingTokens = new StringTokenizer( temp );
    String[] everything = new String[ everythingTokens.countTokens() ];

    for( int i = 0; i < everything.length; i++ ) {
        everything[ i ] = everythingTokens.nextToken();
    }

    int numberOfPeople = Integer.parseInt( everything[ 0 ] ); // Line where exception occurs.
    out.println( everything[ 0 ] );

The error message is this

Exception in thread "main" java.lang.NumberFormatException: For input string: "3monkeys"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at gift1.main(gift1.java:32)
Java Result: 1

Why is this happening and how can I fix it?

StringTokenizer not making tokens with new lines

There are no newline chars in the resulting String . To change that..

String eol = System.getProperty("line.separator");
...
  temp += cString + eol;

Ultimately, I would scrap that code and start again.

  1. Use StringBuilder rather than String +=
  2. But then, why add the first line into a String at all if it is expected to be an integer? Parse it immediately and thereafter ignore the String that was read for the 1st line.

As you're appending each line to a string via temp += cString , what you have is "3" + "monkeys", which is "3monkeys". You can solve this by adding a space after each line; temp += cString + " " .

What happens is you concatenate 3 and monkeys with these

while( f.hasNextLine() ) {
    String cString = f.nextLine();
    temp += cString;
}

so temp now will contain 3monkeys you are removing the new line character

if you format your input file such that each line is a separate token then I don't think you still even need the tokenizer there you just have to add cString to the array or better yet use ArrayList so that you won't have to mind the size of the array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM