为什么程序中有退出的代码java还要抛出异常呢?

ekisstherain 2009-09-26 09:17:41
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(" ","");
都没用啊???这又是为什么啊
谁可以帮忙解释下
谢谢啦...
...全文
224 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ekisstherain 2009-09-27
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 pipi1314yatou 的回复:]
这么专业啊
[/Quote]

???
ekisstherain 2009-09-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 haibo0321 的回复:]
判断改为 if (null == str || "".equals(str))
你可以调试跟一下程序 就会发现当回车的时候 str="" 而不是NULL
[/Quote]

你的解答正确,现在就是怎么去掉空格的问题了
谢谢你啦
pipi1314yatou 2009-09-27
  • 打赏
  • 举报
回复
这么专业啊
ekisstherain 2009-09-27
  • 打赏
  • 举报
回复
代码的功能就是从控制台输入str串,找出串中最大的重复子串,就这样,但是当str=null时,就是说我什么也没有输入来找子串,这样的情况会抛出异常,为什么呢???
另外,我想去掉str串中的空格,怎么用才能去str串中的所有空格呢???
就两个问题,代码是有点长,不过对于我的问题不用看明白应该可以的啦,现在应该明白了吧。。。。
阿奇XS 2009-09-27
  • 打赏
  • 举报
回复
linux+oracle+j2ee 群:87923477
youjianbo_han_87 2009-09-27
  • 打赏
  • 举报
回复
说一下,到底要实现什么功能,在做什么的时候出错了,楼主这么长的代码,我真的没有很多时间去看啊。
BearKin 2009-09-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 bao110908 的回复:]
把代码格式缩进整理好是对看帖者的一种尊重,也能很好地解决问题!
[/Quote]

缩进不缩进不重要 代码这么长 发出来吓唬我们啊..
  • 打赏
  • 举报
回复
把代码格式缩进整理好是对看帖者的一种尊重,也能很好地解决问题!
「已注销」 2009-09-27
  • 打赏
  • 举报
回复
判断改为 if (null == str || "".equals(str))
你可以调试跟一下程序 就会发现当回车的时候 str="" 而不是NULL
tangjun685 2009-09-27
  • 打赏
  • 举报
回复
我来顶
「已注销」 2009-09-27
  • 打赏
  • 举报
回复
完全不明白你想要实现什么效果
ekisstherain 2009-09-27
  • 打赏
  • 举报
回复
呵呵, 不好意思啦,我就是那么粗心啦
答案 就是
str=str.replaceAll(" ","");
这个是把所有的空格都去掉
我还试了一下str=str.trim();
这个只能去掉开头和最后的空格,中间的去不了
比如" a b c "
用了trim后变成"a b c"
就这样了

谢谢各位细心帮忙啦...
分数不多,将就着分啦^_^
「已注销」 2009-09-27
  • 打赏
  • 举报
回复
你说你用过replaceAll 但不行 我想你一定是太粗心了 这个方法返回值是String类型的结果 而不是直接作用在字符串上 下次小心噢! 呵呵
「已注销」 2009-09-27
  • 打赏
  • 举报
回复
你说你用过replaceAll 但不行 我想你一定是太粗心了 这个方法返回值是String类型的结果 而不是直接作用在字符串上 下次小心噢! 呵呵
「已注销」 2009-09-27
  • 打赏
  • 举报
回复
不好意思,刚刚中午休息...
对于你第二的问题,想要消除所有的空格 对吧?
我这里有一个方法,就是用空字符去替代空格
代码如下
str=str.replace(" ","");
你把这句话加在 printString("the str you input is:",str); 这句前就行了

可能还有其他方法,那就你自己再去琢磨琢磨
ekisstherain 2009-09-26
  • 打赏
  • 举报
回复
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);
}
}
ekisstherain 2009-09-26
  • 打赏
  • 举报
回复
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);
}
}
ekisstherain 2009-09-26
  • 打赏
  • 举报
回复
设置成代码形式:

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);
}
}

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧