[英]java.lang.NumberFormatException: For input string: ""-StringBuilder
The main goal of the program is to sum of extract the dollar amount / find the sum of only number which is appended after the $
sign and the sum result should be from the input taken is 50040. The StringBiulder
Object is coming in each loop but when I am trying to convert it to toString
and trying to add it to the sum, getting the error.该程序的主要目标是提取美元金额的总和/找到附加在$
符号之后的唯一数字的总和,并且总和结果应该来自所采用的输入是 50040。每个循环中都会出现StringBiulder
对象,但是当我尝试将其转换为toString
并尝试将其添加到总和时,出现错误。
required is convert the result to a number and add it to the sum that is not happening and I am getting the error:所需的是将结果转换为数字并将其添加到未发生的总和中,我收到错误消息:
java.lang.NumberFormatException: For input string: "" java.lang.NumberFormatException:对于输入字符串:“”
My code:我的代码:
package com.example.demo;
public class DemoApplication2 {
public static void main(String args[]) {
long sum=0;
String str = new String("#1 Tickets $50,000 Received $40. Expenses. ChequE.");
char chararray[] = str.toCharArray();
for (int i = 0; i < chararray.length; i++) {
StringBuilder someString = new StringBuilder();
if (chararray[i] == '$') {
for (int j = i+1; Character.isDigit(chararray[j]) || chararray[j] == ',' ; j++) {
if (Character.isDigit(chararray[j])) {
someString.append(chararray[j]);
}
i=j;
}
}
System.out.println(someString.toString());
sum=sum+Long.parseLong(someString.toString());
}
}
}
Getting the below Error:得到以下错误:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Long.parseLong(Long.java:702)
at java.base/java.lang.Long.parseLong(Long.java:817)
at com.example.demo.DemoApplication2.main(DemoApplication2.java:27)
Here is the result of one of my tests.这是我的一项测试的结果。
The sum is 50040
One way to do this is to go through the String
, checking for a dollar sign.一种方法是通过String
,检查美元符号。 When you find one, you set a boolean switch to true.当您找到一个时,您将布尔开关设置为 true。
If the boolean switch is true, you get the next characters until you get to a stop character (period or space).如果布尔开关为真,您将获得下一个字符,直到到达停止字符(句点或空格)。 I made the assumption based on your code that the number values would all be integer values and not floating-point values.我根据您的代码假设数字值都是整数值而不是浮点值。 Otherwise, you would use a Double
conversion and a double
sum.否则,您将使用Double
转换和double
总和。
I ignored the comma character and collected the digit characters into a StringBuilder
.我忽略了逗号字符并将数字字符收集到StringBuilder
。 I converted the StringBuilder
digits into a long
value.我将StringBuilder
数字转换为long
值。
I put the stop characters into a separate method to document what they represent.我将停止字符放入单独的方法中以记录它们所代表的内容。
I put the StringBuilder
to long
conversion into a separate method because I execute that code twice.我将StringBuilder
long
转换为一个单独的方法,因为我执行了两次该代码。 Once when I detect a stop character and once at the end of the String
.一次是在检测到停止字符时,一次是在String
的末尾。 I check at the end of the String
in case a number value is at the end of the String
with no stop character.我检查String
的末尾,以防数字值位于String
的末尾且没有停止字符。
Here's the complete runnable code.这是完整的可运行代码。
public class DemoApplication2 {
public static void main(String[] args) {
long sum = 0;
String str = new String("#1 Tickets $50,000 Received $40. Expenses. ChequE.");
char chararray[] = str.toCharArray();
boolean isNumber = false;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < chararray.length; i++) {
if (isNumber) {
if (chararray[i] == ',') {
continue;
} else if (Character.isDigit(chararray[i])) {
builder.append(chararray[i]);
} else if (isStopCharacter(chararray[i])) {
sum += valueOf(builder);
isNumber = false;
}
}
if (chararray[i] == '$') {
isNumber = true;
}
}
sum += valueOf(builder);
System.out.println("The sum is " + sum);
}
public static boolean isStopCharacter(char c) {
return c == ' ' || c == '.';
}
public static long valueOf(StringBuilder builder) {
long value = 0L;
if (builder.length() > 0) {
value = Long.valueOf(builder.toString());
builder.delete(0, builder.length());
}
return value;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.