JAVA分割字符串与比较字符串问题

来源:百度知道 编辑:UC知道 时间:2024/06/05 16:42:57
问题一分割字符串程序:
public class Splittest{
String s1 =new String("hello");
public void main(String[] args){
String[] splitresult = s1.split("l");
System.out.println(splitresult);
}
}
以上程序可以创建CLASS文件,但是不能执行,一执行就显示MAIN方法错误,请大家指点下.谢谢

问题二比较字符串程序:
public class StringTest{
String s1 = new String("hello");
String s2 = new String("world");
String s3 = s1.concat(s2);
String s4 = new String("helloworld");
public void main(String[] args){
String s5;
String s6;
s5 = s1.equals(s2);
s6 = s3.equals(s4);
System.out.println(s5);
System.out.println(s6);
}
}
以上比较程序连CLASS文件都不能创立,请大家帮忙看下,最好能好心写详细点,谢谢了.

第一个,你的main方法写错了,你的成员属性在main方法之外如果要引用的话可以把他定义成static
public class Splittest {
static String s1 =new String("hello");
public static void main(String[] args){
String[] splitresult = s1.split("l");
System.out.println(splitresult);
}
}
第二个,除了有第一问题的错误外,还有一个equals方法返回是boolean类型值,你则把他指定为String类型,所以出错
static String s1 = new String("hello");
static String s2 = new String("world");
static String s3 = s1.concat(s2);
static String s4 = new String("helloworld");
public static void main(String[] args){
boolean s5;
boolean s6;
s5 = s1.equals(s2);
s6 = s3.equals(s4);
System.out.println(s5);
System.out.println(s6);
}
你可以使用一些java的编辑工具,jcreater,eclipse
都有一些提醒功能,对写程序很有帮助

///////////////////////////////////
public class Splittest{
public static void main(String[] args){
String s1 = new Strin