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 7662395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:47:28+00:00 2026-05-31T13:47:28+00:00

My code is as such: public static void main(String args[]) throws IOException { String

  • 0

My code is as such:

public static void main(String args[]) throws IOException
{
    String name;
    int population;
    String[] tokens;
    FileReader fin = new FileReader("Test.txt");

    Scanner src = new Scanner(fin);

    src.useDelimiter(",");

    while (src.hasNextLine())
    {
        if (src.hasNext())
        {
            tokens = src.nextLine().split(",\\s");
            name = tokens[0];
            population = Integer.parseInt(tokens[1].trim());
            System.out.printf("Name:%-15s  %-15d\n", name, population);
        } else
        {
            String str = src.next();
            if (str.equals(""))
            {
                break;
            } else
            {
                System.out.println("File format error.");
                return;
            }
        }
    }
    System.out.println("end");
    fin.close();
}

In my test file, I just have a state’s name followed by a comma, a space, the population, and possibly a random space after the population. My output results as

…..
Name:Virginia 7100702
Name:Washington 5908684
Name:West Virginia 1813077
Name:Wisconsin 5371210
Name:Wyoming 495304
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
at javatesters.JavaTesters.main(JavaTesters.java:35)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

The end of my test file is

Virginia 7100702
Washington 5908684
West Virginia 1813077
Wisconsin 5371210
Wyoming 495304

Total Apportionment Population 281424177 435

And I want the blank line to indicate the end of the file

  • 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-05-31T13:47:30+00:00Added an answer on May 31, 2026 at 1:47 pm

    When your scanner gets down to a line that doesn’t include a “, “, that split(",\\s") call splits it into just one string. So, when you access tokens[1], you’re going out of bounds of the array. There are lots of ways to work around this. One approach (though not the cleanest) would be to check to make sure tokens has two elements before you use it.

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

Sidebar

Related Questions

Say I've code like: import java.util.Date; import my.own.Date; class Test{ public static void main(String
what is wrong with such code public List<SearchItem> Search(string find) { return (from i
Considering such code: class ToBeTested { public: void doForEach() { for (vector<Contained>::iterator it =
I have just encountered this following code: public class TestFinally { public static void
Given the following code: class C { static void m(String s) { ... }
Let's consider the following code snippet in Java. package escape; final public class Main
//C++ Example #include <iostream> using namespace std; int doHello (std::string&); int main() { std::string
1 class test { 2 public static int compare0(Comparable x, Comparable y) { 3
I try use a integer array in java with the code below: public static
I find myself writing code such as: foreach($array as $key => $value) { switch($key)

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.