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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:40:30+00:00 2026-06-01T06:40:30+00:00

I seem to be encountering an error when reading from a text file. This

  • 0

I seem to be encountering an error when reading from a text file. This program should read a line, check the first Character, and run the related code in the if statement. The program runs fine for the first line, and outputs the contents, however It cannot process the next line. Here is the code im using:

public void importStart(){

    try {
        FileReader fr = new FileReader("src/data.txt");
        BufferedReader reader = new BufferedReader(fr);

        String line = reader.readLine();
        Scanner scan = null;

            while(line != null){
                scan = new Scanner(line);
                String string1 = scan.next();
                inputType = string1.charAt(0);
                if(inputType == 'S'){
                    foxCount = scan.nextInt();
                    rabbitCount = scan.nextInt();
                    dragonCount = scan.nextInt();
                    System.out.println(inputType + " "+ foxCount + " "+ rabbitCount + " "+ dragonCount);
                }
                else if(inputType == 'X'){
                    System.out.println("Test 1");
                    animalType = string1.substring(2, 3);
                    System.out.println("Test 2");
                        if(animalType == "F"){
                            step = scan.nextInt();

                        }
                        else if(animalType == "R"){
                            step = scan.nextInt();
                        }
                        else if(animalType == "D"){
                            step = scan.nextInt();
                        }
                    System.out.println(inputType + " "+ animalType + " " + step);
                }

             line = reader.readLine();

            }
            reader.close();
        }

and I receive this error

Note, the First line is output that should be there, this is how I know it has operated on the first line correctly. The “Test 1” also appears correctly, which leads me to believe the problem is with my string1.substring implementation. Is this the problem?

S 74 199 15
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException:         String index out of range: 3
Test 1
at java.lang.String.substring(Unknown Source)
at DataDisplayGui.importStart(DataDisplayGui.java:107)
at DataDisplayGui.actionPerformed(DataDisplayGui.java:178)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
  • 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-01T06:40:31+00:00Added an answer on June 1, 2026 at 6:40 am
     String string1 = scan.next();
    

    scan.next() gives you the next token in the line variable, which might not be the whole line. Try replacing the offending substring line with:

    animalType = line.substring(2, 3);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm encountering an error I can't seem to figure out in my bare-bones program
After reading this tidbit , it would seem that the current user's permission would
I seem to remember reading something about how it is bad for structs to
I seem to make this mistake every time I set up a new development
The error is in this code: //myutil.h template <class T, class predicate> T ConditionalInput(LPSTR
I'm implementing a c# program that should automatize a Mono-alphabetic substitution cipher. The functionality
I seem to be encountering a strange issue in Objective-C converting a float to
Newbie question. In Python 2.7.2., I have a problem reading text files which accidentally
I'm encountering a common TableView data reloading problem. I've read many questions on the
This might seem like a pretty detailed question about Easymock, but I'm having a

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.