Topic: 字符串处理问题! |
Print this page |
1.字符串处理问题! | Copy to clipboard |
Posted by: handsome112233 Posted on: 2006-05-26 10:11 我想实现输入“1234567”,输出为“$1,234,567” 下边是我写的程序: import java.lang.*; class ZhuangHuan{ public StringBuffer Zh(StringBuffer buffer){ StringBuffer str=buffer.reverse(); str.append("$"); for(int i=0;i<buffer.length();i++){ i=i+3; str.insert(i,","); } str.reverse(); return str; } public static void main(String [] args){ ZhuangHuan z=new ZhuangHuan(); StringBuffer b=new StringBuffer("1234567"); System.out.println("zhuang huan huo:"+z.Zh(b)); } } 运行时出错,信息如下: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind ex out of range: 11 at java.lang.AbstractStringBuilder.insert(AbstractStringBuilder.java:980 ) at java.lang.StringBuffer.insert(StringBuffer.java:447) at ZhuangHuan.Zh(ZhuangHuan.java:17) at ZhuangHuan.main(ZhuangHuan.java:33) 请各位帮帮忙!谢谢! |
2.Re:字符串处理问题! [Re: handsome112233] | Copy to clipboard |
Posted by: JavaandC Posted on: 2006-05-26 11:32 我从新给你写了一个,可以实现你的功能,你看一下吧。 import java.text.DecimalFormat; public class FormatNumber{ public String Format(String str) { double insert = Double.parseDouble(str); DecimalFormat df = new DecimalFormat("$0,000,000"); return df.format(insert); } public static void main(String [] args) { FormatNumber f = new FormatNumber(); String str= "1234567"; System.out.println("Message:"+f.Format(str)); } } 输出结果: Message : $1,234,567 |
3.Re:字符串处理问题! [Re: handsome112233] | Copy to clipboard |
Posted by: handsome112233 Posted on: 2006-05-26 13:25 谢谢JavaandC,可以解释一下您的程序吗? |
4.Re:字符串处理问题! [Re: handsome112233] | Copy to clipboard |
Posted by: handsome112233 Posted on: 2006-05-26 13:30 还有一个问题想请教! 如果要从控制台读入字符应该怎样做! System.in.read();只能读入一个字节! |
5.Re:字符串处理问题! [Re: handsome112233] | Copy to clipboard |
Posted by: handsome112233 Posted on: 2006-05-26 14:33 我将你的程度作了如下修改,但是出现问题,不知如何解决! import java.text.DecimalFormat; import java.io.*; public class FormatNumber{ public String Format(String str) { double insert = Double.parseDouble(str); DecimalFormat df = new DecimalFormat("$0,000,000,000"); return df.format(insert); } public static void main(String [] args) { FormatNumber f = new FormatNumber(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str=br.readLine(); System.out.println("Message:"+f.Format(str)); } } 出现编译时出现如下问题: D:\JCreator\MyProjects\FormatNumber.java:24: 未报告的异常 java.io.IOException;必须对其进行捕捉或声明以便抛出 String str=br.readLine(); ^ 1 错误 |
6.Re:字符串处理问题! [Re: handsome112233] | Copy to clipboard |
Posted by: JavaandC Posted on: 2006-05-26 14:50 给你的main方法改成 public static void main(String [] args) { FormatNumber f = new FormatNumber(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = null; try { str = br.readLine(); //这句要捕获异常的IOException } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Message:"+f.Format(str)); } |
7.Re:字符串处理问题! [Re: handsome112233] | Copy to clipboard |
Posted by: handsome112233 Posted on: 2006-05-26 14:51 知道了怎样做了!原来要throw IOException! 正确的程序如下! import java.text.DecimalFormat; import java.io.*; public class FormatNumber{ public String Format(String str) { double insert = Double.parseDouble(str); DecimalFormat df = new DecimalFormat("$0,000,000,000"); return df.format(insert); } public String printConsoleLine() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = null; str = br.readLine(); return str; } public static void main(String [] args)throws IOException { FormatNumber f = new FormatNumber(); String str=f.printConsoleLine(); //String str= "1234567899"; System.out.println("Message:"+f.Format(str)); } } |
8.Re:字符串处理问题! [Re: handsome112233] | Copy to clipboard |
Posted by: JavaandC Posted on: 2006-05-26 14:55 最好不要把异常抛给虚拟机,要自己捕获异常,我已经把代码发在上面了,你自己看一下吧。 |
9.Re:字符串处理问题! [Re: handsome112233] | Copy to clipboard |
Posted by: handsome112233 Posted on: 2006-05-26 14:56 public static void main(String [] args) { FormatNumber f = new FormatNumber(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = null; try { str = br.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Message:"+f.Format(str)); } 楼主的方法不行,因为我看到报错后也是这样做的。 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |