我收到有关我的索引超出范围的错误
I am getting an error about my index being out of bounds
我的代码是为了找到两个单词在普通字母处相交的次数,然后打印出交叉的两个单词,如果它们确实交叉。
当我编译时它很好但是当我运行它时我得到一个错误。我知道这与我的索引超出范围有关,但我不知道该怎么办。
public class Assg23
{
public static void main(String[] args)
{
String w1 = args[0];
String w2 = args[1];
int numberOfCrosses = 0;
int pos1 = 0;
int pos2 = 0;
for(int i=0; i < w1.length(); i++)
{
for(int j=0; j < w2.length(); j++)
{
if(w1.charAt(i) == w2.charAt(j))
{
numberOfCrosses++;
crossesAt(w1, pos1, w2, pos2);
printCross(w1, pos1, w2, pos2);
}
}
}
if(numberOfCrosses == 0)
{
System.out.println("Words do not cross");
}
}
private static boolean crossesAt(String w1, int pos1, String w2, int pos2)
{
if(w1.charAt(pos1) == w2.charAt(pos2))
{
return true;
}
else
{
return false;
}
}
private static void printCross(String w1, int pos1, String w2, int pos2)
{
for(int i=0; i < w1.length(); i++)
{
for(int j = 0; j < w2.length(); j++)
{
if(j == pos1)
{
System.out.print(w2.charAt(i));
}
if(i == pos2)
{
System.out.print(w1.charAt(j));
}
else
{
System.out.print(" ");
}
}
}
}
我收到这个错误:
第 59 行是:System.out.print(w2.charAt(i));
第 27 行是:printCross(w1, pos1, w2, pos2);
x-10-250-57-156:csci1103 Katie$ java Assg23 lottery boat
blotto a t Exception in thread "main" `java.lang.StringIndexOutOfBoundsException: String index out of range: 4`
at java.lang.String.charAt(String.java:658)
at Assg23.printCross(Assg23.java:59)
at Assg23.main(Assg23.java:27)
跟踪您的 printCross 算法。
外循环遍历第一个字符串的长度 w1 - ("lottery"),内循环遍历第二个字符串的长度 w2 - ("boat")。
一旦外循环的索引 (i) 大于字符串 w2 的长度,您就不想访问 w2.charAt(i),这正是您在行 System.out.print(w2.charAt(i));
.
这同样适用于行 System.out.print(w1.charAt(j));
,但只有当 j 大于 w1 的长度(即,如果字符串 w1 比字符串 w2 短)时才会出现问题。
我不完全确定我是否理解你正在尝试做的事情,但你可以执行一个简单的检查,例如:
if (j == pos1 && i < w2.length()) { ... }
我的代码是为了找到两个单词在普通字母处相交的次数,然后打印出交叉的两个单词,如果它们确实交叉。
当我编译时它很好但是当我运行它时我得到一个错误。我知道这与我的索引超出范围有关,但我不知道该怎么办。
public class Assg23
{
public static void main(String[] args)
{
String w1 = args[0];
String w2 = args[1];
int numberOfCrosses = 0;
int pos1 = 0;
int pos2 = 0;
for(int i=0; i < w1.length(); i++)
{
for(int j=0; j < w2.length(); j++)
{
if(w1.charAt(i) == w2.charAt(j))
{
numberOfCrosses++;
crossesAt(w1, pos1, w2, pos2);
printCross(w1, pos1, w2, pos2);
}
}
}
if(numberOfCrosses == 0)
{
System.out.println("Words do not cross");
}
}
private static boolean crossesAt(String w1, int pos1, String w2, int pos2)
{
if(w1.charAt(pos1) == w2.charAt(pos2))
{
return true;
}
else
{
return false;
}
}
private static void printCross(String w1, int pos1, String w2, int pos2)
{
for(int i=0; i < w1.length(); i++)
{
for(int j = 0; j < w2.length(); j++)
{
if(j == pos1)
{
System.out.print(w2.charAt(i));
}
if(i == pos2)
{
System.out.print(w1.charAt(j));
}
else
{
System.out.print(" ");
}
}
}
}
我收到这个错误:
第 59 行是:System.out.print(w2.charAt(i)); 第 27 行是:printCross(w1, pos1, w2, pos2);
x-10-250-57-156:csci1103 Katie$ java Assg23 lottery boat
blotto a t Exception in thread "main" `java.lang.StringIndexOutOfBoundsException: String index out of range: 4`
at java.lang.String.charAt(String.java:658)
at Assg23.printCross(Assg23.java:59)
at Assg23.main(Assg23.java:27)
跟踪您的 printCross 算法。
外循环遍历第一个字符串的长度 w1 - ("lottery"),内循环遍历第二个字符串的长度 w2 - ("boat")。
一旦外循环的索引 (i) 大于字符串 w2 的长度,您就不想访问 w2.charAt(i),这正是您在行 System.out.print(w2.charAt(i));
.
这同样适用于行 System.out.print(w1.charAt(j));
,但只有当 j 大于 w1 的长度(即,如果字符串 w1 比字符串 w2 短)时才会出现问题。
我不完全确定我是否理解你正在尝试做的事情,但你可以执行一个简单的检查,例如:
if (j == pos1 && i < w2.length()) { ... }