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

  • Home
  • SEARCH
  • 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 8777303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:14:54+00:00 2026-06-13T19:14:54+00:00

For a uni assignment, I have to take input from a text file and

  • 0

For a uni assignment, I have to take input from a text file and sort it into two separate arrays. The text file is a football league table, arranged as such:
Barcelona 34
Real Madrid 32

I have written a piece of code like this:

holdingString = fileInput.readLine ();                
StringTokenizer sort = new StringTokenizer (holdingString + " "); 
countOfTokens = sort.countTokens();
System.out.println (countOfTokens + " tokens: " + holdingString);

This prints out the number of tokens and what the tokens are for each line, so it gives output of
Two tokens: Barcelona 34
Three tokens: Real Madrid 32

I’ve then written this piece of code:

for (int i = 0; i < countOfTokens; i++)
 {
  String temp = sort.nextToken ();          
  System.out.println(temp);
 }

This reads just the next token and prints it out.
However, rather than printing the next token out, I want to check if it is a word or a number, and separate it into a different array accordingly, so it will be like this:
ArrayTeam Zero Element Barcelona
ArrayTeam First Element Real Madrid
ArrayPoints Zero Element 34
ArrayPoints First Element 32

What’s the easiest way to do this? I’ve tried using a try/catch, but didn’t get it right. I’ve also tried using an if statement with \d, but that’s not worked either.

  • 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-13T19:14:55+00:00Added an answer on June 13, 2026 at 7:14 pm

    Like AmitD, I agree that using split is more appropriate in this case, but if you still like to use a StringTokenizer you do something like:

    StringBuilder teamName=new StringBuilder();
    for (int i = 0; i < countOfTokens-1; i++)
    {
      if (i>0) teamName.append(' ');
      teamName.append(sort.nextToken());          
    }
    teamNames[k]=teamName.toString(); //add the new team to your teamNames array
    points[k]=Integer.parseInt(sort.nextToken()); //if your points array is of int type
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is uni assignment and I have already done some stuff. Please go to
I have to program a simple threaded program with MFC/C++ for a uni assignment.
so we're doing this assignment at Uni and i have a serious craving to
I gave uni association having many to many relationship between two entities, from C
I have 2 uni-dimentional arrays in workspace, one (xarray) is to be used as
As a project over summer while I have some downtime from Uni I am
For my Uni assignment I have to create a fast action paced networked game
I am hacking away at a uni assignment and have come across a problem
I am working on a simple game for a uni assignment, and I have
I have an assignment for uni where I have to create a Java program

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.