[英]Why am I getting a java.lang.StringIndexOutOfBoundsException error when I run?
[英]Why am I getting java.lang.StringIndexOutOfBoundsException?
我想编写一个程序,逐步打印单词,直到出现完整的句子。 例如:我需要编写(输入)并输出:
一世
我需要
我需要
我要写
这是我的代码:
public static void main(String[] args) {
String sentence = "I need to write.";
int len = sentence.length();
int numSpace=0;
System.out.println(sentence);
System.out.println(len);
for(int k=0; k<len; k++){
if(sentence.charAt(k)!='\t')
continue;
numSpace++;
}
System.out.println("Found "+numSpace +"\t in the string.");
int n=1;
for (int m = 1; m <=3; m++) {
n=sentence.indexOf('\t',n-1);
System.out.println("ligne"+m+sentence.substring(0, n));
}
}
这就是我得到的:
我要写
16
在字符串中找到0。
线程“主”中的异常java.lang.StringIndexOutOfBoundsException:
字符串索引超出范围:-1处
java.lang.String.substring(String.java:1937)在
split1.Split1.main(Split1.java:36)Java结果:1构建成功
(总时间:0秒)
我不明白为什么numSpace不计算空格的出现,也不明白为什么我没有得到正确的输出(即使我将numSpace替换为3)。
\\t
字符,因此indexOf(..)
返回-1
解决方法是检查:
if (n > -1) {
System.out.prinltn(...);
}
您寻找numSpace
循环不正确。 您正在寻找一个\\t
,它是一个制表符,字符串中没有。
此外,当您在底部循环时,会得到一个异常,因为您尝试使用同一\\t
进行解析,这将再次不返回任何结果。 n
的值n=sentence.indexOf('\\t',n-1);
将返回-1
,这意味着“没有您要查找的内容的最后一个索引”。 然后,您尝试获取值为-1
的实际子字符串,它是无效的子字符串,因此会出现异常。
您误以为\\t
是水平制表符而不是空白字符(空格)的转义序列。 搜索' '
可以解决问题,并在句子中找到空格。
这看起来像是作业,所以我的回答是一个提示。
提示:阅读String.indexOf
的javadoc时要注意有关未找到字符串/字符时返回的值的说明。
(实际上-即使这不是正式的功课,您显然也是Java的初学者。初学者需要了解,使用不熟悉的方法时, javadocs是查找的第一位。)
我想解决这个问题的最简单方法是先使用String.split函数拆分String。 像这样:
static void sentence(String snt) {
String[] split = snt.split(" ");
for (int i = 0; i < split.length; i++) {
for (int j = 0; j <= i; j++) {
if (i == 1 && j == 0) System.out.print(split[j]);
else System.out.printf(" %s", split[j]);
}
}
}
正如其他人指出的那样。 您正在将制表符(\\ t)以外的每个字符都算作一个空格。 您需要检查空格
if (sentence.charAt(k) == ' ')
\\t
代表制表符 。 要查找空间 ,只需使用' '
。 .indexOf()
如果在字符串中找不到字符,则返回-1。 因此,我们一直循环直到.indexOf()
返回-1。 continue
。 遇到空格时,我们将递增numSpaces
。 System.out.format
很有用。 没有丑女+
š需要。 String sentence = "I need to write.";
int len = sentence.length();
int numSpace = 0;
for (int k = 0; k < len; k++) {
if (sentence.charAt(k) == ' ') {
numSpace++;
}
}
System.out.format("Found %s in the string.\n", numSpace);
int index = sentence.indexOf(' ');
while(index > -1) {
System.out.println(sentence.substring(0, index));
index = sentence.indexOf(' ', index + 1);
}
System.out.println(sentence);
}
试试这个,它几乎应该做您想要的。 我认为您已经完成了这一步,所以我使代码变得非常真实。 阅读注释以了解代码背后的原因。
public static void main(String[] args) {
String sentence = "I need to write.";
int len = sentence.length();
String[] broken = sentence.split(" "); //Doing this instead of the counting of characters is just easier...
/*
* The split method makes it where it populates the array based on either side of a " "
* (blank space) so at the array index of 0 would be 'I' at 1 would be "need", etc.
*/
boolean done = false;
int n = 0;
while (!done) { // While done is false do the below
for (int i = 0; i <= n; i++) { //This prints out the below however many times the count of 'n' is.
/*
* The reason behind this is so that it will print just 'I' the first time when
* 'n' is 0 (because it only prints once starting at 0, which is 'I') but when 'n' is
* 1 it goes through twice making it print 2 times ('I' then 'need") and so on and so
* forth.
*/
System.out.print(broken[i] + " ");
}
System.out.println(); // Since the above method is a print this puts an '\n' (enter) moving the next prints on the next line
n++; //Makes 'n' go up so that it is larger for the next go around
if (n == broken.length) { //the '.length' portion says how many indexes there are in the array broken
/* If you don't have this then the 'while' will go on forever. basically when 'n' hits
* the same number as the amount of words in the array it stops printing.
*/
done = true;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.