0

該程序旨在通過將左括號與右括號進行匹配來錯誤檢查其他源文件。因爲我想在每次我錯誤檢查代碼時使用該程序,所以我創建了一個數組,它將通過掃描器存儲代碼饋送行,然後創建一個方法,然後將其轉換爲字符串,但是當我嘗試來存儲新的字符串,並通過另一個方法傳遞給另一個方法,這會將它拆開並檢查開始和結束括號,它會崩潰,並且堆棧顯示出界限錯誤。我試圖多次修復這個問題,但我無法弄清楚是什麼導致它崩潰。當通過方法傳遞一個字符串參數時,索引超出範圍:1(java)

這是我到目前爲止。我可以得到任何幫助嗎?

堆棧跟蹤:

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); 
    } 
    } 
} 
+0

請記住,數組是0基地索引。 –

+0

你能發佈錯誤跟蹤嗎? – Christian

回答

1

沒有看到一個錯誤跟蹤,很難..但訪問lines[pLines]當尋找@

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

問題是最有可能的。我不知道你的邏輯在嘗試什麼(我沒有讀到那麼遠......只是做了代碼審查),所以你應該重新評估一下。

+0

java.lang.ArrayIndexOutOfBoundsException:1在MatchUp.arrayToString(MatchUp.java:51)在MatchUp.main(MatchUp.java:27)在sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)在sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source)at e.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java)處的java.lang.reflect.Method.invoke(Unknown Source)處的sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) :27 2) –

+0

pLines是一個值,它會在一行被轉換爲一個字符串後遞增,這會改變下一次轉換的數組部分 –

+0

您所指的'lines'數組是單個元素數組。即它的長度爲1,唯一有效的索引爲0.嘗試訪問任何其他索引都會導致出現異常。 –