0

The program is designed to error check other source files, by matching up the opening bracket with the closing bracket. Because I want to use the program each time I'm error checking code, I've created an array that will store the lines of code feed through the scanner and then have a method that then converts it into a string, but when I try to store the new string and pass it through another method that will take it apart and check for the opening and closing braces, it crashes and the stack shows an out of bounds error. I've tried to fix this multiple times, but I can't figure out what is causing it to crash.

Here is what I have so far. Can I get any help?

Stack Trace:

java.lang.ArrayIndexOutOfBoundsException: 1
at MatchUp.arrayToString(MatchUp.java:51)
at MatchUp.main(MatchUp.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

import java.io.*;
import java.util.Scanner;

public class braceMatchup{
public static void main(String[] args) throws IOException{


  Scanner nameDoc = new Scanner(System.in);                              
  System.out.println("Please enter the file name:");
  String fileName = nameDoc.next();                                                        
  File validation = new File(fileName);

  while (!validation.exists()){                                           
    System.out.println("Please enter a valid file name:");
    fileName = nameDoc.next();
    validation = new File(fileName);
  }

  nameDoc.close();

  Scanner doc = new Scanner(new FileReader(fileName));                    
  arrayToString(doc);  
}


public static void arrayToString(Scanner doc){

  for(int tLines = 0; doc.hasNextLine(); tLines++){
    String lines[] = {doc.nextLine()};

    for(int pLines = 0; pLines <= tLines; pLines++){
        String arrayLine = lines[pLines].toString();

        walkArray(arrayLine, tLines);}
    }
}

public static void walkArray(String arrayLine, int tLines){
 int index;            

  for (int i = 0; i <= arrayLine.length(); i++){
    if (arrayLine.substring(i) == "{" || arrayLine.substring(i) == "}")
      index = i;
    else
      index = arrayLine.length();

    bracketCount(arrayLine, index);
  }
}


public static void bracketCount(String arrayLine, int index){

  int openCount = 0; 
  int closeCount = 0;          


  if(arrayLine.substring(index) == "{"){
    openCount++;

    if (index != 0){
      String formatLine = arrayLine.substring(0, index -1); //lines[pLines].indexOf('{')
      System.out.println(formatLine.trim() + " " + "(" + openCount + ")" + "{");}

      else if(index == 0){
        String formatLine = arrayLine.substring(index); //arrayLine.indexOf('{')
        System.out.println(formatLine.trim() + " " + "(" + openCount + ")" + "{");
      }
      else if(arrayLine.substring(index) == "}"){
        closeCount++;

        if(openCount > closeCount)
          closeCount = openCount - 1;
        else if(closeCount > openCount)
          closeCount = 0; 

      if(index != 0){
        String formatLine = arrayLine.substring(0, index - 1); //arrayLine.indexOf('{')
        System.out.println( formatLine.trim() + " " + "}" + "(" + closeCount + ")");
      }
      else if (index == 0){
        String formatLine = arrayLine.substring(0, index - 1); //arrayLine.indexOf('{')
        System.out.println( formatLine.trim() + " " + "}" + "(" + closeCount + ")");}
      }
      else
        System.out.println(arrayLine);
    }
  }
}
2
  • Keep in mind that arrays are 0 base indexed. Commented Oct 26, 2013 at 20:02
  • Can you post the error trace? Commented Oct 26, 2013 at 20:03

1 Answer 1

1

Without seeing an error trace, it's hard.. but looking @

public static void arrayToString(Scanner doc){

for(int tLines = 0; doc.hasNextLine(); tLines++){
  String lines[] = {doc.nextLine()}; // SINGLE ELEMENT ARRAY

  for(int pLines = 0; pLines <= tLines; pLines++){
      String arrayLine = lines[pLines].toString(); // if pLines > 0, you'll be sad

      walkArray(arrayLine, tLines);}}}

The problem is most likely when accessing lines[pLines]. I have no idea what your logic is attempting (I didn't read that far... just did a code review), so you should probably re-evaluate this.

Sign up to request clarification or add additional context in comments.

3 Comments

java.lang.ArrayIndexOutOfBoundsException: 1 at MatchUp.arrayToString(MatchUp.java:51) at MatchUp.main(MatchUp.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:27‌​2)
pLines is a value that would increment after a line was converted to a string, that would change witch portion of the array was converted next
The lines array you are referring to is a single element array. I.e. it has a length of 1 and the only valid index is 0. Trying to access any other index will result in the exception you see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.