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
Related
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.
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(",", ""));
In JMETER I have HTTP query which returns JSON String as following:
{"url":"/some/path?Id=343\u0026"}
I'm trying to parse parameter Id from it with BeanShell sampler:
url = prev.getResponseDataAsString();
int start=url.indexOf('Id=');
int end = url.indexOf('u0026')-1;
newId=url.substring(start,end);
vars.put("newId", newId);
and get an error: Token Parsing Error: Lexical error at line 4, column 25. Encountered: "u" (117), after : "\'s"
Any ideas?
So it seems like backslash confuses the parser. Tried some Java String operations (replaceAll, URLEncoder.encode) - none of them seems to help.
JSON URL contained extra parameter: {"url":"/some/path?Id=343\u0026success=1"}, so following code worked:
url = prev.getResponseDataAsString();
int start=url.indexOf("Id=")+3;
int end = url.indexOf("success=1")-6; //note: "\u0026" is 6 characters
newId=url.substring(start,end);
vars.put("newId", newId);
\u0026 is actually one character, not 6. Change line 3 to
int end = url.indexOf("\u0026");
By the way, I am not sure about your usage of single quotes. I suspect BeanShell is converting from a character array to a String in order to make it work. Best make them double quotes, just in case.
I need to retrieve sum of a String column so i wrote this :
<variable name="totalQt" class="java.lang.Integer" calculation="Sum">
<variableExpression>
<![CDATA[Integer.parseInt($F{QTARTBP}.replaceAll(" ", ""))]]>
</variableExpression>
</variable>
But i'm getting this error :
....
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at
articleBonPreparation_1385730226859_237481.evaluateEstimated(articleBonPreparation_1385730226859_237481:428)
at net.sf.jasperreports.engine.fill.JREvaluator.evaluateEstimated(JREvaluator.java:254)
So there are empty string , so how to use zero in that case , i wrote thiw but it's wrong
:
<variableExpression>
<![CDATA[$F{QTARTBP}.equals("") ? 0 : Integer.parseInt($F{QTARTBP}.replaceAll(" ", ""))]]>
</variableExpression>
Any idea will be appreciated
It would be useful to see what input data you are using, but despite that, it's clear that your code will fail if the value of $F{QTARTBP} is a single space (" "). You could try the following expression instead, which will include the single space in your empty string check and will return 0 instead:
$F{QTARTBP}.replaceAll(" ", "").equals("") ? 0 : Integer.parseInt($F{QTARTBP}.replaceAll(" ", ""))
You should be aware that this is will still fail if any of your input strings cannot be interpreted as an integer.
Jasper are not to calculate - you should generate String in your java-code. And calculate what you need in java too.
Upd. This answer is incorrect. I learned JR more deeper and understand it.
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);