我正在寫一個程序來刪除一個單詞中的重複的字母,它是顯示出界限22行錯誤我無法想象出什麼問題。 繼承人的代碼OutOfBoundsException在InputStream方法
import java.io.*;
public class p23
{
public static void main(String args[])throws IOException
{
String inp , z = "" ;
int len , i ,j , l;
char x ,y ;
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Enter a string");
inp = in.readLine();
inp = inp + ' ';
len = (inp.length())-1;
for (i=0;i<=len;i++)
{
x = inp.charAt(i);
z = z + x ;
l = i + 1 ;
for (j=i ; j<len ; j++)
{
y = inp.charAt(j);
if(x==y)
continue;
else
z = z + y;
}
inp = z ;
z = " " ;
if (inp.charAt(l)==' ')
break ;
}
System.out.println("new String "+inp);
}
}
請建議選擇
for循環條件改變從i <= len個至i
Selva
@Selva參見LEN如何初始化 –