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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杨宗彬 中级黑马   /  2012-7-28 18:25  /  1748 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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 错误 帮忙看下 谢谢了

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

5 个回复

倒序浏览
本帖最后由 纪艺松 于 2012-7-28 21:36 编辑

希望你能看的懂,

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

QQ截图20120728213444.jpg

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
你用方法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;
                        }
                }
        
               
        }

}
回复 使用道具 举报
谢谢楼上了 参数是正则表达式 刚学习了正则表达式"+"号是特殊字符 需要转义
代码这样改比较好String a[] = str.split("[\\+]");
回复 使用道具 举报
  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);不可能有你要的结果。
回复 使用道具 举报
第一个是错误的行,第二个事修改错误行之后的正确截图

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

错误行

错误行

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

修改正确

修改正确
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马