本菜鸟昨天写了书里的一个练习: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();也不会出错。请教各位高手:为什么会发生这种错误?希望能得到详尽的解释,谢谢。

解决方案 »

  1.   

    lengthOfTheSentence变量的值取了0...所以这里charAt()里的参数是-1...这里越界了
      

  2.   

    我在netbean里调了你的程序没问题..yes也可以输出正常的结果,你用的是什么开发环境?
      

  3.   

    对了...你在输入yes后是不是会有多打回车了...就会有theSentence=""
      

  4.   

    开发环境是:jdk1.6.0_05;
    我...是直接在命令提示符里面进行输入的...没有打回车的话好像没有办法继续。高手!你的意思是如果我继续键入回车,就会被String theSentence=keyboard.nextLine(); 这句代码获取了后面那个"",然后theSentence.length()的值就为0吗?
      

  5.   

    原来String theSentence=keyboard.nextLine(); 会直接获取你输入的内容的剩余部分的?
      

  6.   

    sun的api是这样说的:String nextLine()
              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
      

  7.   

    requestAgain=keyboard.next(); 
    你把这句换成keyboard.nextLine(); 就可以了!
      

  8.   

    我这也是ok的eclipse
    估计你打回车了
      

  9.   

    //修改版本
    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(); } }}
      

  10.   

    查到了,书里是这样说的:“无论上一次键盘读入是在哪个地方停止的,Scanner类的方法nextLine都会从这个地方开始读取文本中的剩余内容(remainder)。”也就是跟你说的一样,theSentence="",非常感谢你耐心的调试,记首功啊兄弟。
      

  11.   

    不过我不明白,为什么你用netbeans和11楼的兄弟用eclipse都没有问题,原来的代码应该是会发生这种错误才对的啊~再讨论,现在结贴先~~
      

  12.   

    兄弟,你这样修改好像也会出问题喔~~我的最后方案是: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.nextLine();

    }
        
        }}不过这不是最优编码,只是功能正常而已~~依然感谢~~