Java “如何修复”;StringIndexOutOfBoundsException“;错误?
我需要制作一个程序,打印两个字符串中最长的公共子字符串。 例如:Java “如何修复”;StringIndexOutOfBoundsException“;错误?,java,runtime-error,Java,Runtime Error,我需要制作一个程序,打印两个字符串中最长的公共子字符串。 例如: String str1 = "abcdef"; String str2 = "abcgef"; 最长的公共字符串应该是“abc”。 我只能使用循环、字符串和数组!没有方法/功能等。。我是一个初学者,虽然我知道函数,但不允许使用它 我尝试使用count变量,这样就不会将最后一个字母与第二个字符串中的其他字符反复进行比较,但会出现相同的错误 String com = ""; String com2 = ""; int a; int
String str1 = "abcdef";
String str2 = "abcgef";
最长的公共字符串应该是“abc”
。
我只能使用循环、字符串和数组!没有方法/功能等。。我是一个初学者,虽然我知道函数,但不允许使用它
我尝试使用count变量,这样就不会将最后一个字母与第二个字符串中的其他字符反复进行比较,但会出现相同的错误
String com = "";
String com2 = "";
int a;
int b;
for (i = 0; i < str1.length(); i++) {
int count1 = 0;
int count2 = 0;
for (int j = 0; j < str2.length(); j++) {
a = i;
b = j;
com2 = "";
while (str1.charAt(a) == str2.charAt(b)) {
com2 = com2 + str1.charAt(a);
if (com2.length()>com.length()) {
com = com2;
}
if (a<str1.length()-1) {
a++;
}
if (b<str2.length()-1) {
b++;
}
}
}
}
System.out.println(com);
String com=”“;
字符串com2=“”;
INTA;
int b;
对于(i=0;icom.length()){
com=com2;
}
如果(a由于循环直到a而产生异常,则由于在递增a
后访问str1.charAt(a)
,而没有检查它是否仍在边界内,因此会出现异常。str2.charAt(b)
将while循环保护更改为:
while (a < str1.length() && b < str2.length() && str1.charAt(a) == str2.charAt(b))
while(a
代码中有两个错误:
如果循环变量小于相关字符串长度,则可以在循环变量时增加它们。对于长度为6的str1
,如果a
等于5,这是str1
的最后一个索引,则会出现StringIndexOutOfBoundsException(与b
/str2
相同)
在while
循环结束时,您不会重新初始化com2
您的代码应该是:
String com = "";
String com2 = "";
int a;
int b;
for (i=0; i<str1.length(); i++) {
int count1 = 0;
int count2 = 0;
for (int j=0; j<str2.length(); j++) {
a = i;
b = j;
while (str1.charAt(a) == str2.charAt(b)) {
com2 = com2 + str1.charAt(a);
if (com2.length()>com.length()) {
com = com2;
}
if (a<str1.length() - 1) {
a++;
}
if (b<str2.length() - 1) {
b++;
}
}
com2 = "";
}
}
System.out.println(com);
String com=”“;
字符串com2=“”;
INTA;
int b;
对于(i=0;ipublic class Main
{
公共静态void main(字符串[]args)
{
字符串com=”“;
字符串com2=“”;
String str1=“bowbow”//随意初始化
字符串str2=“HeloobowHellooo”;
INTA;
int b;
for(inti=0;icom.length())
{
com=com2;
}
a++;
b++;
}
}
}
System.out.println(com);
}
}
进行了一些更改,并在代码中对此进行了注释。似乎工作正常您看起来像这样
String str1="abcdef";
String str2="abcgefghj";
String com = "";
int min =Math.min(str1.length(), str2.length());
for (int i =0; i< min ; i++)
{
if(str1.charAt(i) == str2.charAt(i))
{
com = com + str1.charAt(i);
}
else {
break;
}
}
System.out.println(com);
String str1=“abcdef”;
字符串str2=“abcgefghj”;
字符串com=”“;
int min=Math.min(str1.length(),str2.length());
对于(int i=0;i
如上所述,存在一些编译错误(请尝试使用IDE,这会有所帮助)。
清理完这些后,我做了一些更改,应该可以使用:
String str1 = "abcdef";
String str2 = "abcgef";
String com = "";
String com2 = "";
int a;
int b;
for (int i = 0; i < str1.length(); i++) {
//counts removed (never used)
for (int j = 0; j < str2.length(); j++) {
a = i;
b = j;
com2 = ""; // Reset before start a new Check Loop
while (str1.charAt(a) == str2.charAt(b)) {
com2 = com2 + str1.charAt(a);
if (com2.length() > com.length()) {
com = com2;
}
/**
* length() goes from 0 (empty String) to n
* index 0 is the first char in that String
* so you need to adjust that (the simple way is -1)
*/
if(a < str1.length()-1) {
a++;
}
if(b < str2.length()-1) {
b++;
}
//check for end of String -> Exit loop
if(a >= str1.length()-1 && b >= str2.length()-1) {
break;
}
}
}
}
System.out.println(com);
}
String str1=“abcdef”;
字符串str2=“abcgef”;
字符串com=”“;
字符串com2=“”;
INTA;
int b;
对于(int i=0;icom.length()){
com=com2;
}
/**
*length()从0(空字符串)变为n
*索引0是该字符串中的第一个字符
*所以你需要调整它(简单的方法是-1)
*/
if(a退出循环
如果(a>=str1.length()-1&&b>=str2.length()-1){
打破
}
}
}
}
System.out.println(com);
}
您发布的代码有不同的编译时错误。请修复它们并发布a。如果您的字符串包含3个字符(a、b和c),则此字符串的最后一个可访问索引将为2!如果代码缩进正确,您将帮助自己和其他人。您还应该注意,这不会给出正确的答案,因为您在while循环之前没有重置com2
的值。这是错误的。a@BackSlash它有条件if(a
,因此,虽然a++=>a=6=>while(str1.charAt(6)==error
length()-1确实解决了运行时错误,但程序仍然无法运行。例如,对于字符串“abcd”和“abc”,它会打印“abcbcc”…就我在帖子中写的内容而言,它没有打印任何内容。@LiorRoz因为你有无尽的while循环。我用你所说的+将“String com2=”“;”行放在内部循环中解决了它。我的谢意,但它仍然只对一些字符串有效。。在字符串“abcdef”和“abcgf”上它没有作用anything@LiorRoz我还没接电话
String str1="abcdef";
String str2="abcgefghj";
String com = "";
int min =Math.min(str1.length(), str2.length());
for (int i =0; i< min ; i++)
{
if(str1.charAt(i) == str2.charAt(i))
{
com = com + str1.charAt(i);
}
else {
break;
}
}
System.out.println(com);
String str1 = "abcdef";
String str2 = "abcgef";
String com = "";
String com2 = "";
int a;
int b;
for (int i = 0; i < str1.length(); i++) {
//counts removed (never used)
for (int j = 0; j < str2.length(); j++) {
a = i;
b = j;
com2 = ""; // Reset before start a new Check Loop
while (str1.charAt(a) == str2.charAt(b)) {
com2 = com2 + str1.charAt(a);
if (com2.length() > com.length()) {
com = com2;
}
/**
* length() goes from 0 (empty String) to n
* index 0 is the first char in that String
* so you need to adjust that (the simple way is -1)
*/
if(a < str1.length()-1) {
a++;
}
if(b < str2.length()-1) {
b++;
}
//check for end of String -> Exit loop
if(a >= str1.length()-1 && b >= str2.length()-1) {
break;
}
}
}
}
System.out.println(com);
}