import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class FindMaxRepeatString 
{
/*print the string
 *the parameter explain is the declaring words,
 *and the string is for printing
 */
public static void printString(String explain,String str)
{
System.out.print(explain);
System.out.println(str);
}

//the function is output integer
public static void printString(String explain,int Int)
{
System.out.print(explain);
System.out.println(Int);
}
//the main function
public static void main(String args[])
{
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
String str = null;//define a str for save the input string
String sub=null; //safe a sub string from the str
System.out.println("Input a string :");
try
{
str=buf.readLine();
}
catch(IOException e)
{
System.err.println("Input error:"+e.getMessage());
}

//print the string which you input
printString("the str you input is:",str);
                  //当str=null时,退出程序
if(str==null)
System.exit(0);

int position=0; //record the start point of the sub string
int maxCount=0; //Save the max equal count
//Save the length of the sub string which is repeat max in string
int maxLen=1;
sub=str.substring(0,0);
int i=1;
try 
{
// TODO: handle exception
for(int k=0;k<str.length()-1;k++) //k表示从第几位开始比较
{
for(i=1;i<=str.length()/2&&(k+i)<str.length();i++)
{
sub=str.substring(k, k+i);
System.out.println("the sub is :"+sub);
int count=0;
for(int j=i+k;sub.length()<=str.length()-j;j+=sub.length())
{
String s=str.substring(j,j+i);
System.out.println("the s is :"+s);
if(sub.compareTo(s)==0)
count++;
else
break;
}
if(maxCount<count||(count>0&&maxCount==count&&maxLen<sub.length()))
{
maxCount=count;
position=k;
System.out.println("position is :"+position);
maxLen=sub.length();
}
}
}
}
catch(NullPointerException e)
{
System.err.println("Main: Null point error:"+e.getMessage());
}
                  //这句代码抛出异常
sub=str.substring(position, position+maxLen);
printString("the start of the sub in the string is:", position);
printString("the length of the sub is:", maxLen);
printString("the compare most count is:", maxCount);
printString("The sub string repeat most is:", sub);
}
}我就是不明白为什么
我还成了这样:
if(str!=null)
   sub=str.substring(position, position+maxLen);
它还是要抛出异常,为什么呢???
另外:我用str.trim();为什么不能去掉空格呢???
还用过了str.replaceAll(" ","");
都没用啊???这又是为什么啊
谁可以帮忙解释下
谢谢啦...

解决方案 »

  1.   

    设置成代码形式:import java.io.BufferedReader; 
    import java.io.InputStreamReader; 
    import java.io.IOException; 
    public class FindMaxRepeatString 

    /*print the string 
    *the parameter explain is the declaring words, 
    *and the string is for printing 
    */ 
    public static void printString(String explain,String str) 

    System.out.print(explain); 
    System.out.println(str); 
    } //the function is output integer 
    public static void printString(String explain,int Int) 

    System.out.print(explain); 
    System.out.println(Int); 

    //the main function 
    public static void main(String args[]) 

    BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); 
    String str = null;//define a str for save the input string 
    String sub=null; //safe a sub string from the str 
    System.out.println("Input a string :"); 
    try 

    str=buf.readLine(); 

    catch(IOException e) 

    System.err.println("Input error:"+e.getMessage()); 
    } //print the string which you input 
    printString("the str you input is:",str); 
                      //当str=null时,退出程序 
    if(str==null) 
    System.exit(0); int position=0; //record the start point of the sub string 
    int maxCount=0; //Save the max equal count 
    //Save the length of the sub string which is repeat max in string 
    int maxLen=1; 
    sub=str.substring(0,0); 
    int i=1; 
    try 

    // TODO: handle exception 
    for(int k=0;k <str.length()-1;k++) //k表示从第几位开始比较 

    for(i=1;i <=str.length()/2&&(k+i) <str.length();i++) 

    sub=str.substring(k, k+i); 
    System.out.println("the sub is :"+sub); 
    int count=0; 
    for(int j=i+k;sub.length() <=str.length()-j;j+=sub.length()) 

    String s=str.substring(j,j+i); 
    System.out.println("the s is :"+s); 
    if(sub.compareTo(s)==0) 
    count++; 
    else 
    break; 

    if(maxCount <count||(count>0&&maxCount==count&&maxLen <sub.length())) 

    maxCount=count; 
    position=k; 
    System.out.println("position is :"+position); 
    maxLen=sub.length(); 




    catch(NullPointerException e) 

    System.err.println("Main: Null point error:"+e.getMessage()); 

                      //这句代码抛出异常 
    sub=str.substring(position, position+maxLen); 
    printString("the start of the sub in the string is:", position); 
    printString("the length of the sub is:", maxLen); 
    printString("the compare most count is:", maxCount); 
    printString("The sub string repeat most is:", sub); 

      

  2.   

    import java.io.BufferedReader; 
    import java.io.InputStreamReader; 
    import java.io.IOException; 
    public class FindMaxRepeatString 

    /*print the string 
    *the parameter explain is the declaring words, 
    *and the string is for printing 
    */ 
    public static void printString(String explain,String str) 

    System.out.print(explain); 
    System.out.println(str); 
    } //the function is output integer 
    public static void printString(String explain,int Int) 

    System.out.print(explain); 
    System.out.println(Int); 

    //the main function 
    public static void main(String args[]) 

    BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); 
    String str = null;//define a str for save the input string 
    String sub=null; //safe a sub string from the str 
    System.out.println("Input a string :"); 
    try 

    str=buf.readLine(); 

    catch(IOException e) 

    System.err.println("Input error:"+e.getMessage()); 
    } //print the string which you input 
    printString("the str you input is:",str); 
                      //当str=null时,退出程序 
    if(str==null) 
    System.exit(0); int position=0; //record the start point of the sub string 
    int maxCount=0; //Save the max equal count 
    //Save the length of the sub string which is repeat max in string 
    int maxLen=1; 
    sub=str.substring(0,0); 
    int i=1; 
    try 

    // TODO: handle exception 
    for(int k=0;k <str.length()-1;k++) //k表示从第几位开始比较 

    for(i=1;i <=str.length()/2&&(k+i) <str.length();i++) 

    sub=str.substring(k, k+i); 
    System.out.println("the sub is :"+sub); 
    int count=0; 
    for(int j=i+k;sub.length() <=str.length()-j;j+=sub.length()) 

    String s=str.substring(j,j+i); 
    System.out.println("the s is :"+s); 
    if(sub.compareTo(s)==0) 
    count++; 
    else 
    break; 

    if(maxCount <count||(count>0&&maxCount==count&&maxLen <sub.length())) 

    maxCount=count; 
    position=k; 
    System.out.println("position is :"+position); 
    maxLen=sub.length(); 




    catch(NullPointerException e) 

    System.err.println("Main: Null point error:"+e.getMessage()); 

                      //这句代码抛出异常 
    sub=str.substring(position, position+maxLen); 
    printString("the start of the sub in the string is:", position); 
    printString("the length of the sub is:", maxLen); 
    printString("the compare most count is:", maxCount); 
    printString("The sub string repeat most is:", sub); 

      

  3.   

    import java.io.BufferedReader; 
    import java.io.InputStreamReader; 
    import java.io.IOException; 
    public class FindMaxRepeatString 

    /*print the string 
    *the parameter explain is the declaring words, 
    *and the string is for printing 
    */ 
    public static void printString(String explain,String str) 

    System.out.print(explain); 
    System.out.println(str); 
    } //the function is output integer 
    public static void printString(String explain,int Int) 

    System.out.print(explain); 
    System.out.println(Int); 

    //the main function 
    public static void main(String args[]) 

    BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); 
    String str = null;//define a str for save the input string 
    String sub=null; //safe a sub string from the str 
    System.out.println("Input a string :"); 
    try 

    str=buf.readLine(); 

    catch(IOException e) 

    System.err.println("Input error:"+e.getMessage()); 
    } //print the string which you input 
    printString("the str you input is:",str); 
                      //当str=null时,退出程序 
    if(str==null) 
    System.exit(0); int position=0; //record the start point of the sub string 
    int maxCount=0; //Save the max equal count 
    //Save the length of the sub string which is repeat max in string 
    int maxLen=1; 
    sub=str.substring(0,0); 
    int i=1; 
    try 

    // TODO: handle exception 
    for(int k=0;k <str.length()-1;k++) //k表示从第几位开始比较 

    for(i=1;i <=str.length()/2&&(k+i) <str.length();i++) 

    sub=str.substring(k, k+i); 
    System.out.println("the sub is :"+sub); 
    int count=0; 
    for(int j=i+k;sub.length() <=str.length()-j;j+=sub.length()) 

    String s=str.substring(j,j+i); 
    System.out.println("the s is :"+s); 
    if(sub.compareTo(s)==0) 
    count++; 
    else 
    break; 

    if(maxCount <count||(count>0&&maxCount==count&&maxLen <sub.length())) 

    maxCount=count; 
    position=k; 
    System.out.println("position is :"+position); 
    maxLen=sub.length(); 




    catch(NullPointerException e) 

    System.err.println("Main: Null point error:"+e.getMessage()); 

                      //这句代码抛出异常 
    sub=str.substring(position, position+maxLen); 
    printString("the start of the sub in the string is:", position); 
    printString("the length of the sub is:", maxLen); 
    printString("the compare most count is:", maxCount); 
    printString("The sub string repeat most is:", sub); 


      

  4.   

    判断改为   if (null == str || "".equals(str))
    你可以调试跟一下程序 就会发现当回车的时候 str="" 而不是NULL
      

  5.   

    代码的功能就是从控制台输入str串,找出串中最大的重复子串,就这样,但是当str=null时,就是说我什么也没有输入来找子串,这样的情况会抛出异常,为什么呢???
    另外,我想去掉str串中的空格,怎么用才能去str串中的所有空格呢???
    就两个问题,代码是有点长,不过对于我的问题不用看明白应该可以的啦,现在应该明白了吧。
      

  6.   

    不好意思,刚刚中午休息...
    对于你第二的问题,想要消除所有的空格 对吧?
    我这里有一个方法,就是用空字符去替代空格
    代码如下
    str=str.replace(" ","");
    你把这句话加在 printString("the str you input is:",str); 这句前就行了可能还有其他方法,那就你自己再去琢磨琢磨
      

  7.   

    你说你用过replaceAll 但不行 我想你一定是太粗心了 这个方法返回值是String类型的结果 而不是直接作用在字符串上  下次小心噢! 呵呵
      

  8.   

    你说你用过replaceAll 但不行 我想你一定是太粗心了 这个方法返回值是String类型的结果 而不是直接作用在字符串上  下次小心噢! 呵呵
      

  9.   

    呵呵, 不好意思啦,我就是那么粗心啦
    答案 就是
    str=str.replaceAll(" ","");
    这个是把所有的空格都去掉
    我还试了一下str=str.trim();
    这个只能去掉开头和最后的空格,中间的去不了
    比如"   a  b  c  "
    用了trim后变成"a  b  c"
    就这样了谢谢各位细心帮忙啦...
    分数不多,将就着分啦^_^