背景:我有一个数字1000.我想要的结果是输入1000到一个函数并获得“1千”作为输出。我正在使用1000作为示例,该函数可以使用任何数字执行此操作。
首先我拿这个号码并加上逗号:
public String formatNumbers(String input) {
String number = input;
double dispString = Double.parseDouble(number);
DecimalFormat formatter = new DecimalFormat("#,###");
return (formatter.format(dispString));
}
这会返回逗号为1,000的数字。我将其存储到名为numberwithcommas
其次,我将数字完全转换为单词using this code。我将此变量命名为originalNum
现在我拆分并添加到numberwithcommas
String[] parts = originalNum.split(",");
接下来我分配一个新变量:
String startNum = parts[0];
这给了我结果的前半部分(数字),在这种情况下为1。
现在我将startNum
转换为使用同一类&在originalNum
这会将字符串One
分配给我定义为replace
String replace = NumberToWords.start(Integer.valueOf(startNum));
现在我的爆炸物准备好了我使用简单的替换策略
我使用空字符串One
替换One Thousand
:
String second_half_words = numberInWords.replace(replace, " ");
此时我的second_half_words
等于Thousand
现在我连接startNum和second_half_words来构建我的最终字符串。我得到了一个合适的结果。但是,我的申请的错误报告如下:
java.lang.NumberFormatException: Invalid int: "10 000 000"
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parse(Integer.java:374)
at java.lang.Integer.parseInt(Integer.java:365)
at java.lang.Integer.parseInt(Integer.java:331)
at java.lang.Integer.valueOf(Integer.java:489)
at com.exs.FragmentTab1$CurAdapter.displayStats(FragmentTab1.java:768)
第768行是指
String replace = NumberToWords.start(Integer.valueOf(startNum));
在我的代码中,我完全感到困惑的是,startNum的值在这一点上可能是“10 000 000”。这里一定有什么问题?
以下是整个代码:
public void displayStats(String number){
long intnumber = Long.valueOf(number);
String numberwithcommas = formatNumbers(number);
String numberInWords = NumberToWords.start(intnumber);
String originalNum = numberwithcommas;
String[] parts = originalNum.split(",");
System.out.println("Parts"+Arrays.toString(parts));
String startNum = parts[0]; //This is the top text
System.out.println("Parts2"+startNum);
String replace = NumberToWords.start(Integer.valueOf(startNum));
String second_half_words = numberInWords.replace(replace, " ");
setDispStatsPart1(startNum);
setDispStatswords(second_half_words);
}
public String dispStatsPart1(){
return part1+"\n";
}
public String dispStatswords(){
if(finnumberInWords.length() <= 1){
return "";
}else{
return finnumberInWords+"\n";
}
}
public void setDispStatsPart1(String part){
part1 = part.replaceAll("\\s+", " ");
}
public void setDispStatswords(String words){
finnumberInWords = words.replaceAll("\\s+", " ");
}
public String formatNumbers(String input) {
String number = input;
double dispString = Double.parseDouble(number);
DecimalFormat formatter = new DecimalFormat("#,###");
return (formatter.format(dispString));
}
我不确定在捕获错误报告时该函数的输入必须是什么。但我相信Invalid int: "10 000 000"
出现在第768行,引用
String replace = NumberToWords.start(Integer.valueOf(startNum));
。
以下是我的开发控制台上报告的原始堆栈跟踪:
java.lang.NumberFormatException: Invalid int: "10 000 000"
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parse(Integer.java:374)
at java.lang.Integer.parseInt(Integer.java:365)
at java.lang.Integer.parseInt(Integer.java:331)
at java.lang.Integer.valueOf(Integer.java:489)
at com.exd.FragmentTab1$CurAdapter.displayStats(FragmentTab1.java:768)
at com.exd.FragmentTab1$CurAdapter.bindView(FragmentTab1.java:4158)
at android.widget.CursorAdapter.getView(CursorAdapter.java:254)
at com.exd.FragmentTab1$CurAdapter.getView(FragmentTab1.java:746)
at android.widget.AbsListView.obtainView(AbsListView.java:2301)
at android.widget.ListView.makeAndAddView(ListView.java:1801)
at android.widget.ListView.fillDown(ListView.java:697)
at android.widget.ListView.fillSpecific(ListView.java:1360)
at android.widget.ListView.layoutChildren(ListView.java:1631)
at android.widget.AbsListView.onLayout(AbsListView.java:2157)
at android.view.View.layout(View.java:14981)
at android.view.ViewGroup.layout(ViewGroup.java:4790)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14981)
at android.view.ViewGroup.layout(ViewGroup.java:4790)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14981)
at android.view.ViewGroup.layout(ViewGroup.java:4790)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:887)
at android.view.View.layout(View.java:14981)
at android.view.ViewGroup.layout(ViewGroup.java:4790)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14981)
at android.view.ViewGroup.layout(ViewGroup.java:4790)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
at android.view.View.layout(View.java:14981)
at android.view.ViewGroup.layout(ViewGroup.java:4790)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14981)
at android.view.ViewGroup.layout(ViewGroup.java:4790)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1997)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1752)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1005)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5647)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5426)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)