[英]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.