A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 我为你着迷 金牌黑马   /  2014-4-30 22:54  /  846 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. //练习一:模拟一个trim方法,去除两端的空格。

  2. class StringTest
  3. {
  4.                 public static void sop(String str)
  5.                 {
  6.                                         System.out.println(str);
  7.                 }
  8.        
  9.                 public static void main(String[] args)
  10.                 {
  11.                       String s="     ab cd   ";
  12.                       sop("("+s+")");
  13.                       s=myTrim(s);
  14.                       sop("("+s+")");
  15.                 }

  16.     public static  String myTrim(String str)
  17.     {
  18.                     int start=0,end=str.length()-1;
  19.                    
  20.                     while(start<=end&&str.charAt(start)==' ');
  21.                         start++;
  22.                         
  23.               while(start<=end&&str.charAt(end)==' ');
  24.                   end--;
  25.                  
  26.               return str.substring(start,end+1);
  27.                             
  28.     }
复制代码
大家好,不知道大伙有没有遇到像我这样的情况的,就是这个代码虚拟机编译的时候是可以通过的,但是运行时候去除空格的部分编译不出来,只能编译没有去除空格之前的。另外代码是和视频一模一样的,这让我很郁闷,难不成是我的虚拟机挂掉了嘛! 有遇到这种情况的麻烦讲解下,3Q。


6 个回复

倒序浏览
  1. package string;


  2. class StringTest3
  3. {
  4.                 public static void sop(String str)
  5.                 {
  6.                      System.out.println(str);
  7.                 }
  8.         
  9.                 public static void main(String[] args)
  10.                 {
  11.                       String s="     ab cd   ";
  12.                       sop("("+s+")");
  13.                       System.out.println("1111111111111111111111");
  14.                       s=myTrim(s);
  15.                       System.out.println("22222222222222222");//根本就没有执行到这里,说明MyTrim方法有问题
  16.                       sop("("+s+")");
  17.                 }

  18.     public static  String myTrim(String str)
  19.     {
  20.                     int start=0,end=str.length()-1;
  21.                     System.out.println("444444444444444444");//能执行到这里
  22.                     while(start <= end && str.charAt(start)==' ')//说明问题在这里,发现你多写两个分号,这是个循环语句,不应该结束
  23.                     System.out.println("33333333333333333333333333");  //这一行却不执行了
  24.                         start++;
  25.                         
  26.               while(start<=end&&str.charAt(end)==' ');
  27.                   end--;
  28.                   
  29.               return str.substring(start,end+1);
  30.                              
  31.     }
  32.     }
复制代码


所以综上来说是第22行代码多了个分号    这就是错误的原因,注意细节,尽量不要在这种地方出错!!!!
回复 使用道具 举报
姜姗姗 发表于 2014-4-30 23:56
所以综上来说是第22行代码多了个分号    这就是错误的原因,注意细节,尽量不要在这种地方出错!!!! ...

啊 谢谢啊  原 来是这个问题啊
回复 使用道具 举报
class StringTest
{
        public static void sop(String str){
                System.out.println(str);
    }
    public static void main(String[] args){
         String s="     ab cd   ";
         sop("("+s+")");
         s=myTrim(s);
         sop("("+s+")");
    }

    public static  String myTrim(String str){
                int start=0,end=str.length()-1;
                while(start<=end&&str.charAt(start)==' ');   //不应加“;”
                         start++;
                while(start<=end&&str.charAt(end)==' ');  //不应加“;”
                         end--;
                return str.substring(start,end+1);                        
    }
}

同学,你这和老师的码不一样,多了两处“;”程序没走这两个判断呀,你试试。

回复 使用道具 举报
来男. 发表于 2014-5-1 00:08
class StringTest
{
        public static void sop(String str){

嗯 知道了  太粗心了 谢谢提醒啊
回复 使用道具 举报
我为你着迷 发表于 2014-5-1 00:10
嗯 知道了  太粗心了 谢谢提醒啊

没事,我也太慢了,上面的美眉先答了,我刚才都没看见,哎..
回复 使用道具 举报
来男. 发表于 2014-5-1 00:13
没事,我也太慢了,上面的美眉先答了,我刚才都没看见,哎..

那咱们男人要超过她呀   别被姑娘落下啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马