| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 678 人关注过本帖
标题:求大神帮忙找茬,就是改不过来错误
只看楼主 加入收藏
笔墨痕干
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2014-3-24
结帖率:84.21%
收藏
已结贴  问题点数:20 回复次数:2 
求大神帮忙找茬,就是改不过来错误
package 模拟去掉字符串两端的空格;

import java.util.Scanner;
class trim1{
    String str=null;
    public trim1(String str){
        this.str=str;
        int count1=leftTrim();
        int count2=rightTrim();
        str.substring(count1, count2+1);
        System.out.println(str);
    }
    int leftTrim(){
        int i=0;
        while(true){
            if(str.charAt(i)==" ")
                i++;
            else
                break;
        }
        return i;
    }
    int rightTrim(){
        int leng=str.length();
        while(true){
            if(str.charAt(leng)==" ")
                leng--;
            else
                break;
        }
        return leng;
    }
   
   
   
}
public class Trim {
    public static void main(String[] args){
        String str=null;
        System.out.println("请输入字符串");
        Scanner scan=new Scanner(System.in);
        str=scan.nextLine();
        new trim1(str);
        
    }
   

}




Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Incompatible operand types char and String

    at 模拟去掉字符串两端的空格.trim1.leftTrim(Trim.java:16)
    at 模拟去掉字符串两端的空格.trim1.<init>(Trim.java:8)
    at 模拟去掉字符串两端的空格.Trim.main(Trim.java:43)
charAt()这个方法应该没有用错把!
搜索更多相关主题的帖子: package public return import 字符串 
2015-04-04 21:36
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
收藏
得分:20 
程序代码:
public class TrimTest {
    public static void main(String... args) {
        printTestValue(null);
        printTestValue("");
        printTestValue(" ");
        printTestValue("  ");
        printTestValue("   ");
        printTestValue("1 2  ");
        printTestValue("  1 2");
        printTestValue("  1 2  ");
    }

    public static void printTestValue(String testValue) {
        System.out.println("[" + testValue + "]" + "[" + trimLeftSpace(testValue) + "]" + "[" + trimRightSpace(testValue) + "]" + "[" + trimLeftAndRightSpace(testValue) + "]");
    }

    public static String trimLeftSpace(final String inputValue) {
        if (inputValue == null) {
            return null;
        }

        int index = 0;
        while (index < inputValue.length()) {
            if (inputValue.charAt(index) != ' ') {
                break;
            }
            index++;
        }
        return inputValue.substring(index, inputValue.length());
    }

    public static String trimRightSpace(final String inputValue) {
        if (inputValue == null) {
            return null;
        }

        int index = inputValue.length();
        while (index > 0) {
            if (inputValue.charAt(index - 1) != ' ') {
                break;
            }
            index--;
        }
        return inputValue.substring(0, index);
    }

    public static String trimLeftAndRightSpace(final String inputValue) {
        if (inputValue == null) {
            return null;
        }

        return trimLeftSpace(trimRightSpace(inputValue));
    }
}


程序代码:
[null][null][null][null]
[][][][]
[ ][][][]
[  ][][][]
[   ][][][]
[1 2  ][1 2  ][1 2][1 2]
[  1 2][1 2][  1 2][1 2]
[  1 2  ][1 2  ][  1 2][1 2]
2015-04-04 23:01
笔墨痕干
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2014-3-24
收藏
得分:0 
回复 2楼 日知己所无
好羡慕!!谢谢了
2015-04-06 16:05
快速回复:求大神帮忙找茬,就是改不过来错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015843 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved