using JSTL and JSP to convert string into integer - java

I am using an x:forEach to loop through an XML object to extract data
In the x:forEach I am using x:set to select the values I want.
<x:forEach var="data" select="$path/">
<x:set var="dataPoint" select="string($data//cell[8]/text())" /> ...
As you can see, I am selecting the text within the specified node and then casting it into a string. The dataPoint variables are in fact numbers, and I need to do certain things to them such as sorting and extracting the min and max amounts.
The problem is I am trying to form an Array of integers and my compiler is complaining that I cannot convery an Object into an int.
The error is: " Type mismatch: cannot convert from Object to int "
Any thoughts?
Thanks

Solution:
I cast the Object into a String and then from a String to an integer:
Integer.parseInt(StringVar);

Related

How to create a dynamic array with 2 different data types

I need to create a dynamic array with data from a Excel spreadsheet using Apache-POI and Selenium.
My goal is to be able to create a dynamic array with 2 data types(int and String's)to be called to be inputted into a text field using Selenium WebDriver. I have already gotten the information to be hardcoded, however I'd like to be able to not rely on the workbook to increase the speed of my program.
General structure:
for(int i = 0; i < sheet1.getLastRowNum(); i++) {
string cell[i] = formatter.formatCellValue(sheet1.getRow(i).getCell(0)
}
The errors I get are, "Syntax Error on token "i", delete this token" and also "Type mismatch: Cannot convert from "String" to "String[]"
Would it work if you stored everything in the array as a string? You could just use String.valueOf() to convert the cell value to a string, and if you need to get it back later on as an int you could use Integer.parseInt().
You can make an array of Objects, but that could cause more trouble than it's worth. You could be adding an object into it which has a type you never accounted for, which could cause you problems later on down the line.

How can I get JSONArray values from JSONP?

I am creating an Android app that searches for recipes. I am trying to call an API which returns ingredients as JSONP. I need this data for validation purposes, so the user can't enter any invalid ingredients.
I want to get the values from the JSONArray and return the searchValue. But I do not know how to do this as the format of the data is in JSONP and not JSON.
Below is an example of what I am trying to retrieve.
set_metadata('ingredient',
[{"searchValue":"salt","description":"salt","term":"salt"},{"searchValue":"butter","description":"butter","term":"butter"},
When I tried to simply get the array, there was a JSONException. For debugging, i only wanted to return the length of the array, to see how many ingredients were inside the array.
JSONArray ingr = json.getJSONArray("");
System.out.println("Size of the array is: " + ingr.length());
But it didn't like the characters at the start and it gave me this error.
02-29 21:47:38.568 1993-2416/com.example.laptop.whatsfordinner E/JSON Parserīš• Error parsing data org.json.JSONException: Value set_metadata('ingredient' of type java.lang.String cannot be converted to JSONObject
So how can I "ignore" the first part and get values from the JSONArray?
You may do it with simple string operations, substring based on the first index of [ and the last index of ] .
Not too safe or elegant, but as long as the surrounding javascript has more or less known to you (it starts with set_metadata('ingredient', and ends with ); ) you are good to go with it.

java.lang.NumberFormatException: Invalid int: "3546504756", what does this error mean?

I'm creating an Android app, and I'm reading some coordinates from a text file.
I'm using Integer.parseInt(xCoordinateStringFromFile) to convert the X coordinates to integers, and in the same way with the Y coordinates.
When I run the app, I get an error on that line, which looks like this:
BridgeData data = new BridgeData(
elements[0],
elements[1],
Integer.parseInt(elements[2]),
Integer.parseInt(elements[3]),
Integer.parseInt(elements[4]),
new GeoPos(Integer.parseInt(elements[5].split(",")[0]), Integer.parseInt(elements[5].split(",")[1])),
new GeoPos(Integer.parseInt(elements[6].split(",")[0]), Integer.parseInt(elements[6].split(",")[1])),
Integer.parseInt(elements[7]),
Integer.parseInt(elements[8])
);
The variable elements is a String array created by splitting the current line on every ;.
The "main" error is:
java.lang.NumberFormatException: Invalid int: "3546504756"
I wonder what this means, and how I can solve it.
Error just means that java is not able to convert the String that you are trying to use in your call to Integer.pasrseInt as that number is out of range of an integer.
You should be using Long.parseLong as 3546504756 number is out of range of an integer.
Make sure post that your BridgeData constructor accepts long as a parameter instead of integer.
Revising the concept of data type and their size might help you
http://www.tutorialspoint.com/java/java_basic_datatypes.htm
In Java, an int is 32 bits, which is enough to store numbers up to just over 2 billion. The number you were trying to read was an invalid int because it was too big.
I would seriously question the design of whatever you are doing, if you have coordinates with values of over a billion. But if you really need such big numbers, use long in place of int in your BridgeData class, and Long.parseLong in place of Integer.parseInt in the code that you quoted.
The range of int value can be lies between -2,147,483,648 to 2,147,483,647 and you are providing it more than that thats why it giving numberformatexception
You have to store the value in either long or other more range premetive type.
You can find more about java premetive data type range and value here

why do i have to use Integer.parseInt?

I am new to Java so forgive me if this is a silly question.
First I did the following (this is a section from my code):
public static void team()
{
int score = JOptionPane.showInputDialog("Enter the team score...");
calculate(score);
}
But I kept getting an error saying: "Incompatible types string cannot be converted to int".
So I thought I may need to use parsing. I did the following and it worked:
public static void team()
{
int myScore;
String score = JOptionPane.showInputDialog("Enter the team score...");
myScore = Integer.parseInt(score);
calculate(myScore);
}
I would like to know why there is a need for me to parse the variable "score"? In the first piece of code, I declared the variable score as an integer, so would it not store the input which I expect to be an Integer in the variable score which is then passed into the method calculate. I understand that parsing is when you convert a String into an Integer. However in this case, I do not expect the user to input a string so why is there a need for parsing?
The simple reason is: because JOptionPane.showInputDialog() returns a String, regardless of what you actually want the user to input.
There's no mechanism in Java to do automatic conversion based on the type of your destination variable myScore (though some other languages can do this, e.g. implicit conversions in Scala).
You could (in principle) create a custom dialog that returns an int , e.g. by getting the user to choose from a pulldown list of numbers, and then no parsing would be needed. But otherwise, you have to do parsing explicitly.
The JOptionPane.showInputDialog("Enter the team score..."); returns a String which you tried to store in a int. That is why you are getting the "incompatible types".
See Javadocs for more information.
Because a string is stored differently than an integer. Integers are stored in binary, while as a String is a collection of characters.
Example:
The number 14 would be stored as 1110 in Binary. However, as a String, it's stored as 00110001 00110100
Take a look at this http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/The_Characters.asp
Integer.parseInt() basically does the dirty work for you, by looking up the characters in a lookup table, and as long as they correlate with proper integer values, it coverts it over to binary form, to be stored in an integer.

BIRT: Number type (formats) hell

Birt supports 3 number/digits types (e.g. for output columns in dataset):
Integer
Decimal
Float
At the same time, BIRT has the only 1 (yes! ONE!!!) css build-in format for numbers. That means if you have, for instance int (days) and decimal (amounts) you can't use build-in css style for them (you have define own and apply them on each item).
Go further. You call Java class from BIRT and pass to it so called Integer value. What you get at Java? Correct Double. At it's no way to now at that level that originally we have Integer value.
Of course in some cases you could try to guess data type based on a value.
E.g.
public Object formatFromBirt(Object pObject){
if (pObject == null) return null;
...
if (pObject instanceof Double) {
// workaround for int as double (e.g. days in arrear in total)
LWDecimal dec = new LWDecimal((Double)pObject);
if (dec.getScale() == 0){
pObject = dec.getJavaDecimal().toBigInteger();
}
}
return formatObject(pObject);
}
My BIG Question is - guys, how you handle all that hecking mess in BIRT number type system?
In BIRT API, they have this property definitions.
In general, if you are going to check the source code, in every "property" or "column" or "field" or whatever you call that object as long as it has properties that include its data type, you will notice that one of this object field is the string name of the data type. So it seems that the API verifies the selected data type by this string field.
For example, if you create a Output Column in DataSet and selected decimal as its data type,
the Display name in the drop down is actually in CamelCase which is "Decimal", but the XML name is "decimal".Doing this programmatically, you could try
resultSetColumn.setDataType("Decimal");
So in this same way you can verify its exact type programmatically.
Below is the list of common data type for a DataSet column.
http://www.eclipse.org/birt/ref/rom/structs/ResultSetColumn.html#Property-dataType
UPDATE:
You could use DesignChoiceConstant's static constants, it includes the datatype available to BIRT.It also includes property names and values that you could use to other BIRT APIs such as css,labels, list of allowed values, etc.

Categories

Resources