求大神帮忙找茬,就是改不过来错误
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()这个方法应该没有用错把!
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)