Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8260799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:10:51+00:00 2026-06-08T03:10:51+00:00

I am having trouble making a StringTokenizer break up a few lines of text

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:10:52+00:00Added an answer on June 8, 2026 at 3:10 am

    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 + " ".

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble making a secondary MySQL connection (to a separate, external DB) in
I'm having trouble making this work. Apparently, i can't use > or < in
I'm new to python and experimenting a bit but having trouble making a list
I'm using grep to extract parts from a file but I'm having trouble making
i have used observed_field for arrays of data... but i am having trouble making
I am using a plugin called jQuery contextMenu but am having trouble making it
The picking itself is easy, but I'm having trouble making sure I can't place
Im having trouble making my table contents vertically aligned and was wondering if you
Im having trouble making sure my active graphic is positioned outside of the button,
I'm having trouble making divs top position be relative to its preceding subling. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.