Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Super Contributor
pg1181
Posts: 398
Registered: ‎01-12-2011
My Device: Q10
Accepted Solution

ParseInt Odd Behavior

Hi.

 

I'm completely baffled. The following code will not put anything into my database.

 

String sCurrentTempF = String.valueOf(table.get("CurrentTempF"));

 

int currentTempF = Integer.Parse(sCurrentTempF);

 

sCurrentTempF =  String.valueOf(sCurrentTempF);

 

Statement statement = db.createStatement(INSERT INTO WeatherConditions VALUES('" + sCurrentTempF + "'));

statement.prepare();

statement.execute();

statement.close();

 

Yet the following will:

 

String sCurrentTempF = String.valueOf(table.get("CurrentTempF"));

 

//int currentTempF = Integer.Parse(sCurrentTempF);

 

//sCurrentTempF =  String.valueOf(sCurrentTempF);

 

Statement statement = db.createStatement(INSERT INTO WeatherConditions VALUES('" + sCurrentTempF + "'));

statement.prepare();

statement.execute();

statement.close();

 

It's not liking my Integer.Parse();

 

Is this method somehow changing the original value???

Contributor
marKram
Posts: 33
Registered: ‎02-13-2010
My Device: 9800

Re: ParseInt Odd Behavior

I think there is a typo in your code. The variable "currentTempF" is never used in the first snippit.

Super Contributor
pg1181
Posts: 398
Registered: ‎01-12-2011
My Device: Q10

Re: ParseInt Odd Behavior

[ Edited ]

Yeah I think you're right, but that was a type on my part, the code is fine just doesn't work. Here is what I should've typed:

 

String sCurrentTempF = String.valueOf(table.get("CurrentTempF"));

 

int currentTempF = Integer.ParseInt(sCurrentTempF);

 

sCurrentTempF =  String.valueOf(currentTempF);

 

Statement statement = db.createStatement(INSERT INTO WeatherConditions VALUES('" + sCurrentTempF + "'));

statement.prepare();

statement.execute();

statement.close();

 

It seems that as soon as I play with Integer.ParseInt nothing gets put into my DB, whether I use the resulting value or not.

 

Really strange.

 

Anyways the point of this whole waste of time is to take a STRING like 14.3 and turn it into another STRING 14.

I figured I'd use the truncating power of the INT but I guess I'm not allowed if I want anything to go into my database.

Contributor
marKram
Posts: 33
Registered: ‎02-13-2010
My Device: 9800

Re: ParseInt Odd Behavior

Integer.parseInt will give you a numberFormatException if you pass in a string like "14.3". It will only parse a integer value in a string. If you just want to truncate you can do this:

 

 

 
float f = Float.parseFloat(sCurrentTempF);
int i = (int) f;
sCurrentTempF = String.valueOf(i);

 

 

Super Contributor
pg1181
Posts: 398
Registered: ‎01-12-2011
My Device: Q10

Re: ParseInt Odd Behavior

Thanks, it works. The problem is I don't know if the data coming back (from our .Net web service) will be double or int. It will always be in string format but it could contain a double or int number. I've noticed now that if it's int, then this code prevents that entry from being loaded into my DB.

 

If I use Float.parseFloat("14") what happens??

Contributor
marKram
Posts: 33
Registered: ‎02-13-2010
My Device: 9800

Re: ParseInt Odd Behavior

Just a hint. Try making a regular Java project in Eclipse and writing some small Junits to test these cases. If you are just using standard Java classes then the behaviour is (pretty much) the same between the regular JRE and the BlackBerry JRE.

 

Even if you just write a simple class with a void main you can see what happens with a simple sysout... :-)

Contributor
marKram
Posts: 33
Registered: ‎02-13-2010
My Device: 9800

Re: ParseInt Odd Behavior

PS. If you want to actually round the number up or down you can look at MathUtilities.round() as long as your are using JRE 4.6 or higher.

Super Contributor
pg1181
Posts: 398
Registered: ‎01-12-2011
My Device: Q10

Re: ParseInt Odd Behavior

Wow thanks.

 

I actually found out that my problem had to do with some of the records in my DB containing NULL or Empty String. I just added some validation to my code and it seems to be working ok right now.

 

Thanks for the rounding up or down suggestion that might come in handy.

New Member
mohdshakeel1
Posts: 1
Registered: ‎05-03-2011
My Device: 8520 curve

Re: ParseInt Odd Behavior

how to use ineternet 

 

Developer
peter_strange
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: ParseInt Odd Behavior

@pg1181 - Can you mark this as solved then?

 

The Help link at the top will assist you finding out how to do this if you don't already know.