Cannot parse "1,000.00" String to Number - java

I have several labels displaying numeric values and I need to parse these texts to number. The problem is that when the value is greater than 999, the parse method fails throwing the following exception:
Exception in thread "AWT-EventQueue-0"
java.lang.NumberFormatException: For input string: "1,000.00" at
java.lang.NumberFormatException.forInputString(Unknown Source)
I tried several parse methods like Double.valueOf(string), new BigDecimal(string), new BigInteger(string) and so on...but the exception is always thrown.

I guess you are using a French numbers.
You can add a Locale to NumberFormat and parse in the documentation :
NumberFormat.getNumberInstance(Locale.FRANCE).parse("1,000")

Remove the comma before parsing like this:
double d = Double.parseDouble(string.replace(",", ""));

Related

java.lang.IllegalArgumentException: Invalid format: "2018-08-24T��:��:��" is malformed at "��:��:��"

The front end is sending the date with an invalid time format and I get this exception:
Caused by: java.lang.IllegalArgumentException: Invalid format:
"2018-08-24T��:��:��" is malformed at "��:��:��" at
org.joda.time.format.DateTimeParserBucket.doParseMillis(DateTimeParserBucket.java:187)
at
org.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:826)
at
org.joda.time.convert.StringConverter.getInstantMillis(StringConverter.java:65)
at org.joda.time.base.BaseDateTime.(BaseDateTime.java:173)
at org.joda.time.DateTime.(DateTime.java:257)
Basically I want to check if the timestamp has any malformed data. If yes, I want to set it to 0. For example the datetime is 2018-08-24T��:��:�� I want to set it to 2018-08-24T00:00:00
You are already half-way there.
try {
.. your code that parses the FED input
} catch ( IllegalArgumentException e) {
.. do further checking
Meaning: that exception is already giving you, well, an exception, when you received bad input from your source. In that case, you could a simply substring() or regex check on the incoming string. And if it goes "4digits dash 2 digits dash 2 digits" ... then you got a date, and can create a corresponding object manually, and fill in the time values to all 0 for example.

Replace backslash in string

I have a String in which I am trying to replace the number enclosed by two backslashes. For example: \10\ , I am trying to replace that with 10. I am currently using this regex to do that:
String texter = texthb.replaceAll("\\.+\\", "\\"+String.valueOf(pertotal + initper)+"\\");
This line is giving the following error:
Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Unexpected internal error near index 4
.+\
I know it is because the regex is wrong. What is the proper way to accomplish this? Thanks in advance.
Use four backslashes to match a single backslash character.
String texter = texthb.replaceAll("\\\\.+?\\\\", "\\\\"+String.valueOf(pertotal + initper)+"\\\\");

String to Int with a + sign

I'm parsing stock data and trying to put it into a SQL database. All of the info from the parse is retrieved as a string. I am using the Integer.parseInt() method to try and convert the strings to integers for some of the info. The issue I am having is with the Change data. When it is a positive change the number has a "+" sign in front of it and I am getting an error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "+0.14" //
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) //
at java.lang.Integer.parseInt(Integer.java:492) //
at java.lang.Integer.parseInt(Integer.java:527) //
at getStockData.main(getStockData.java:91)" (the //s are to signify end lines, having issues with formatting)
My Output is:
Ticker ID: MSFT: Change - [+0.14]
int Change = Integer.parseInt(di.getTextContent());
I don't really know how to get around this error at the moment, and haven't found anything similar to this after googling / searching stackoverflow.
The issue is 0.14 is not a valid int. Try using Double.parseDouble(String) to parse the double value. Like
double v = Double.parseDouble("+0.14");
System.out.println(v);
Output is
0.14

Converting from XMLSchema datatype to Java int

I am working on a project that involves fetching data from Dbpedia and I was wondering whether there is anyway to convert the the object returned from a dbpedia query i.e a XMLSchema#double into a java int so that I can perform operations on it and modify the data for my use. I am using jena to fetch the data from the sparql endpoints jena provide. I tried using the toString method to change the RDFnode into a string and than converting into an int/double, but that doesnt seem to work and gives me the exception that is listed below:
Exception in thread "main" java.lang.NumberFormatException: For input string: "147181000000^^http://www.w3.org/2001/XMLSchema#double"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
Does anyone here have a work around this problem??
After Converting to String it look like:
"147181000000^^http://www.w3.org/2001/XMLSchema#double"
And you want to convert into int/double for that you have to separate only first number from this string
Number= 147181000000 .
You can use indexOf or split method to get Number from the String.
Sample Code:
String number = "147181000000^^http://www.w3.org/2001/XMLSchema#double";
number = number.subString(0,nuber.indexOf(^));
int num = Integer.ParseInt(number);

java.util.UnknownFormatConversionException:

System.out.printf("%s%13s%\n", "TarrifType", "AnnualCost");
System.out.printf("%s%d.%n", "String" 243.08);
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '
at java.util.Formatter.checkText(Unknown Source)
at java.util.Formatter.parse(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at ModelComparison.main(ModelComparison.java:12)
Any idea whats wrong?
What's wrong is the %\n in the first line. Note that the % is a special character in a format string that indicates that a format specifier follows. The \n after the % is not a valid format specifier.
If you wanted to print a percent sign, then double it in the format string: %%
If you wanted to print a newline, then use %n, not %\n.
The problem in your format string is that you mixed two ways of doing newline: %n and \n. The former tells the formatter to put a newline in whatever format the platform requires, whereas the latter puts in just a literal newline char. But what you wrote was %\n, which means you're escaping the newline char, and that's what's blowing up.
You also forgot a comma between "String" and 243.08 in the second call. And btw, %d formats an integer, so you probably don't want it if you're trying to print 243.08.
Bugs..
System.out.printf("%s%13s\n", "TarrifType", "AnnualCost");
System.out.printf("%s%f\n", "String", 243.08);
http://ideone.com/USOx1
In my case (Android) there was an error in strings.xml:
<string name="cashback">10% cashback</string>
When I tried to get getString(R.string.cashback), I received this exception. To fix it, replace % with %%.
If a string desired to format in Kotlin, the following code snippet can be used.
In the string.xml:
<string name = "exampleString"><![CDATA[<font color=#3177a3> String_1: <b>%%s</b><br> String_2: <b>%%s</b><br> String_3: <b>%%s</b></font>]]></string>
In the main code:
val format = String.format(getString(R.string.exampleString),
value1,
value2,
value3
)

Categories