Java编程异常 [英] Java programming exception
本文介绍了Java编程异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮助我吗?我不理解此异常:
Can any one help me? I do not understand this exception:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 9
at java.lang.String.charAt(Unknown Source)
at charPerLine.main(charPerLine.java:13)
这是负责的代码:
import java.util.*;
public class charPerLine {
public static void main(String[] args)throws StringIndexOutOfBoundsException {
Scanner sc=new Scanner(System.in);
System.out.print("Type any name:");
String s=sc.next();
int j= s.length()+1;
for(int i=0;i<=j;i++){
System.out.println(s.charAt(i));
}
}
}
推荐答案
您试图直接访问少于10个字符的字符串的第10个字符。像这样的东西:
You're trying to directly access the 10th character of a string which has less than 10 characters. Something like:
"12345".charAt(9)
请记住,字符串索引基于0,因此 .charAt(9)
=>第10个字符。因此 123 .charAt(3)
也会抛出。
Remember that String indices are 0-based, hence .charAt(9)
=> 10th character. So "123".charAt(3)
would throw, too.
这篇关于Java编程异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文