簡體   English   中英

索引超出范圍:1(java)當通過方法傳遞字符串參數時

[英]index out of bounds: 1 (java) when passing a string paramater through a method

該程序旨在通過將左括號和右括號匹配來對其他源文件進行錯誤檢查。 因為我想在每次對代碼進行錯誤檢查時都使用該程序,所以我創建了一個數組,該數組將存儲通過掃描儀輸入的代碼行,然后有一個方法將其轉換為字符串,但是當我嘗試時要存儲新字符串並通過另一種方法將其拆開並檢查開括號和右括號,它會崩潰,並且堆棧顯示出界錯誤。 我已嘗試多次修復此問題,但是我無法弄清楚是什么導致它崩潰。

這是我到目前為止所擁有的。 我可以幫忙嗎?

堆棧跟蹤:

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

沒有看到錯誤跟蹤,這很難..但是看@

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

訪問lines[pLines]時,此問題最有可能發生。 我不知道您的邏輯正在嘗試什么(我沒有讀那么遠……只是進行了代碼審查),所以您可能應該重新評估一下。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM