Question

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)
Was it helpful?

Solution

 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);

OTHER TIPS

Depends what's in your src/data.txt file. If the line you read was

X

you'd get this error because the statement

string1.substring(2,3)

is trying to return a substring, starting at character 2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top