Java 字符串索引超出范围?
我的代码不断出现以下错误: “线程主java.lang.stringindexoutofboundsexception字符串索引中的异常超出范围” 有什么问题Java 字符串索引超出范围?,java,Java,我的代码不断出现以下错误: “线程主java.lang.stringindexoutofboundsexception字符串索引中的异常超出范围” 有什么问题 import java.util.Scanner; public class Compress { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a
import java.util.Scanner;
public class Compress {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a string: ");
String compress = scan.nextLine();
int count = 0;
for (count = 0; count <= compress.length(); count++)
{
while( count +1 < compress.length() && compress.charAt(count) == compress.charAt(count + 1))
{
count = count + 1;
}
System.out.print(count + "" + compress.charAt(count));
}
}
import java.util.Scanner;
公共类压缩{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入字符串:”);
字符串压缩=scan.nextLine();
整数计数=0;
对于(count=0;count字符串索引从0
运行到length-1
,因此您正在运行字符串的末尾compress
。请从
for (count = 0; count <= compress.length(); count++)
for(count=0;count字符串索引从0
运行到length-1
,因此您正在运行字符串的末尾compress
。将for
循环条件从
for (count = 0; count <= compress.length(); count++)
for(count=0;count索引范围从0到length-1
。请尝试使用:-
for (count = 0; count < compress.length(); count++)
for(count=0;count
而不是
for (count = 0; count <= compress.length(); count++)
for(count=0;count索引范围从0到length-1
。请尝试使用:-
for (count = 0; count < compress.length(); count++)
for(count=0;count
而不是
for (count = 0; count <= compress.length(); count++)
for(计数=0;计数