本菜鸟昨天写了书里的一个练习:import java.util.*;public class ReactToASentence{ public static void main(String[] args){
Scanner keyboard=new Scanner(System.in);
String requestAgain="yes"; while(requestAgain.equalsIgnoreCase("yes")){ System.out.println("Enter a sentence with a pounctuation at the end:"); String theSentence=keyboard.nextLine(); int lengthOfTheSentence=theSentence.length(); char pounctuation=theSentence.charAt(lengthOfTheSentence-1); int remainder=lengthOfTheSentence%2; if((pounctuation=='?')&&(remainder==0)){
System.out.println("Yes");
}else if((pounctuation=='?')&&(remainder==1)){
System.out.println("No");
}else if(pounctuation=='!'){
System.out.println("Wow");
}else{
System.out.println("You always say \""+theSentence+"\"");
}
System.out.println("Would you like again?(yes/no)"); requestAgain=keyboard.next();
}
}}编译没有问题,第一次运行循环没有发现问题,但当询问Would you like again?(yes/no),然后本人输入yes的时候,命令提示符显示出现这样的错误:Exception in thread "main" java.lang.StringIndexOutOfBoundsException:String index out of range:-1at java.lang.String.charAt(Unknown Source)
at ReactToASentence.main(ReactToASentence.java:19)输入no的时候程序则顺利结束;本菜鸟也试过将最后一行代码改为requestAgain=keyboard.nextLine();也不会出错。请教各位高手:为什么会发生这种错误?希望能得到详尽的解释,谢谢。
Scanner keyboard=new Scanner(System.in);
String requestAgain="yes"; while(requestAgain.equalsIgnoreCase("yes")){ System.out.println("Enter a sentence with a pounctuation at the end:"); String theSentence=keyboard.nextLine(); int lengthOfTheSentence=theSentence.length(); char pounctuation=theSentence.charAt(lengthOfTheSentence-1); int remainder=lengthOfTheSentence%2; if((pounctuation=='?')&&(remainder==0)){
System.out.println("Yes");
}else if((pounctuation=='?')&&(remainder==1)){
System.out.println("No");
}else if(pounctuation=='!'){
System.out.println("Wow");
}else{
System.out.println("You always say \""+theSentence+"\"");
}
System.out.println("Would you like again?(yes/no)"); requestAgain=keyboard.next();
}
}}编译没有问题,第一次运行循环没有发现问题,但当询问Would you like again?(yes/no),然后本人输入yes的时候,命令提示符显示出现这样的错误:Exception in thread "main" java.lang.StringIndexOutOfBoundsException:String index out of range:-1at java.lang.String.charAt(Unknown Source)
at ReactToASentence.main(ReactToASentence.java:19)输入no的时候程序则顺利结束;本菜鸟也试过将最后一行代码改为requestAgain=keyboard.nextLine();也不会出错。请教各位高手:为什么会发生这种错误?希望能得到详尽的解释,谢谢。
我...是直接在命令提示符里面进行输入的...没有打回车的话好像没有办法继续。高手!你的意思是如果我继续键入回车,就会被String theSentence=keyboard.nextLine(); 这句代码获取了后面那个"",然后theSentence.length()的值就为0吗?
Advances this scanner past the current line and returns the input that was skipped.
这个侦听器会把当前行传递进来并返回所跳过的内容http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
你把这句换成keyboard.nextLine(); 就可以了!
估计你打回车了
package test;import java.util.Scanner;public class ReactToASentence {
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String requestAgain = "yes"; while (requestAgain.equalsIgnoreCase("yes")) { System.out.println("Enter a sentence with a pounctuation at the end:"); //String theSentence = keyboard.nextLine();//直接获取你输入的内容的下一行 ,相当打了回车
String theSentence = keyboard.next();//改成这样就可以了
int lengthOfTheSentence = theSentence.length(); char pounctuation = theSentence.charAt(lengthOfTheSentence - 1); int remainder = lengthOfTheSentence % 2; if ((pounctuation == '?') && (remainder == 0)) { System.out.println("Yes"); } else if ((pounctuation == '?') && (remainder == 1)) { System.out.println("No"); } else if (pounctuation == '!') { System.out.println("Wow"); } else { System.out.println("You always say \"" + theSentence + "\""); } System.out.println("Would you like again?(yes/no)"); requestAgain = keyboard.next(); } }}
Scanner keyboard=new Scanner(System.in);
String requestAgain="yes"; while(requestAgain.equalsIgnoreCase("yes")){ System.out.println("Enter a sentence with a pounctuation at the end:"); String theSentence=keyboard.nextLine(); int lengthOfTheSentence=theSentence.length(); char pounctuation=theSentence.charAt(lengthOfTheSentence-1); int remainder=lengthOfTheSentence%2; if((pounctuation=='?')&&(remainder==0)){
System.out.println("Yes");
}else if((pounctuation=='?')&&(remainder==1)){
System.out.println("No");
}else if(pounctuation=='!'){
System.out.println("Wow");
}else{
System.out.println("You always say \""+theSentence+"\"");
}
System.out.println("Would you like again?(yes/no)"); requestAgain=keyboard.nextLine();
}
}}不过这不是最优编码,只是功能正常而已~~依然感谢~~