DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   question on java (http://www.daniweb.com/forums/thread157240.html)

kdhanshree Nov 14th, 2008 1:42 am
question on java
 
I add integer values in combo box.When I call getSselectedItem it return object.How i convert it into integer.

sillyboy Nov 14th, 2008 1:54 am
Re: question on java
 
I assume you are after JavaScript, not Java. Anyway with JavaScript values are not strongly typed, so if the values are numbers, you can use "+".

stultuske Nov 14th, 2008 4:57 am
Re: question on java
 
Quote:

Originally Posted by kdhanshree (Post 735662)
I add integer values in combo box.When I call getSselectedItem it return object.How i convert it into integer.

casting it to an Integer. but from your question I take it you want an int, and not an Integer.
so, cast it to a String, and parse that to an int.

for instance, you get back an Object with value = "6";

public int objectToInt(Object givenObject){
  String inString = (String)givenObject;
  return Integer.parseInt(inString);
}

't voila, instant integer-value. this will only work if the value stored in the object is a valid integer value, so if you have any other data in that list, you may want to catch possible exceptions.

Ezzaral Nov 14th, 2008 12:32 pm
Re: question on java
 
If you added the items as Integer objects:
int selectedValue = ((Integer)comboBox.getSelectedItem()).intValue();


All times are GMT -4. The time now is 12:10 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC