黑马程序员技术交流社区

标题: 关于string的东西 帮忙看看 [打印本页]

作者: 杨宗彬    时间: 2012-7-28 18:25
标题: 关于string的东西 帮忙看看
package com.yzb.String;

public class StringTest2 {

        /**
         * @param args
         */
        public static void main(String[] args) throws Exception{
                // TODO Auto-generated method stub
                int ch = 0;
                int pos = 0;
                String str = null;
                byte[] buf = new byte[1024];
                System.out.println("Please input info");
                while(true){
                        ch = System.in.read();
                        switch (ch) {
                        case '\r':
                                break;
                        case '\n':
                                str = new String(buf,0,pos);
                                String a[] = str.split("+");
                                int result = 0;
                                for (int i = 0; i < a.length; i++) {
                                        int temp = Integer.parseInt(a[i]);
                                        result = result + temp;
                                }
                                System.out.println(result);
                                break;
                        default:buf[pos++] = (byte)ch;
                                break;
                        }
                }
       
               
        }

}
我想在输出回车换行得到str后用split方法截取字符串然后实现累加哪点错了报Dangling meta character '+' near index 0 错误 帮忙看下 谢谢了
作者: 纪艺松    时间: 2012-7-28 21:23
本帖最后由 纪艺松 于 2012-7-28 21:36 编辑

希望你能看的懂,

QQ截图20120728213444.jpg (61.61 KB, 下载次数: 46)

QQ截图20120728213444.jpg

作者: 任文杰    时间: 2012-7-28 22:03
你用方法split(regex)传参数不正确,用+进行对字符串切割,虽然我们在连接字符串的时候用+号,但+并不是字符串的一部分。楼主的意图可能是读取取键盘输入的数字串,并计算各数字字符的累加和。可以用正则表达式来完成。下面是我对楼主的程序做的一些小修整。仅供参考,相互学习,共同进步。

public class StringTest2 {

        /**
         * @param args
         */
        public static void main(String[] args) throws Exception{
                // TODO Auto-generated method stub
                int ch = 0;
                int pos = 0;
                String str = null;
                byte[] buf = new byte[1024];
                System.out.println("Please input info");
                while(true){
                        ch = System.in.read();
                        
                        switch (ch) {
                        case '\r':
                                break;
                        case '\n':
                                str = new String(buf,0,pos);
                              
                                
                               // String a[] = str.split("");                                
                                Pattern p = Pattern.compile("[0-9]");
                                Matcher m = p.matcher(str);
                                int result = 0;
                                while(m.find()) {
                                        int temp = Integer.parseInt(m.group());
                                        result = result + temp;
                                }
                           //     String a[] = str.
                                /*int result = 0;
                                for (int i = 0; i < a.length; i++) {
                                        int temp = Integer.parseInt(a);
                                        result = result + temp;
                                }*/
                                System.out.println(result);
                                pos = 0;
                                break;
                        default:buf[pos++] = (byte)ch;
                                break;
                        }
                }
        
               
        }

}
作者: 杨宗彬    时间: 2012-7-29 10:40
谢谢楼上了 参数是正则表达式 刚学习了正则表达式"+"号是特殊字符 需要转义
代码这样改比较好String a[] = str.split("[\\+]");

作者: 陆强强    时间: 2012-7-29 21:26
  ch = System.in.read();//read返回的是int,下面怎么能用字符来做判断标记
                        switch (ch) {
                        case '\r':
                                break;
                        case '\n':
                                str = new String(buf,0,pos);//System.In没有在buf存入过东西所以你这语句就变成了
str = new String(null,0,pos);不可能有你要的结果。
作者: 王峰    时间: 2012-7-30 10:08
第一个是错误的行,第二个事修改错误行之后的正确截图

未命名.jpg (10.91 KB, 下载次数: 28)

错误行

错误行

正确截图,对照修改.jpg (12.21 KB, 下载次数: 26)

修改正确

修改正确





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2