Conversion of Object to int

Hi

I am getting NumberFormatException while converting Object to int.

I ve tried with this,

ArrayList list = getData();

Iterator i =list.iterator();

while(i.hasNext()){

Object o = (Object)i.next();

int k = Integer.parseInt((String)o); or

int k = Integer.parseInt(o.toString());

System.out.println("k :" + k);

int n=Integer.parseInt(o.toString());

if(n==Integer.parseInt(value.toString())){

System.out.println("Duplicate");

It doesn't work.can anyone help me?//

}

}

[831 byte] By [rose@rosea] at [2007-11-27 10:22:22]
# 1

Your number format exception will be with your Integar.parseInt() method...

I'd double check what your arraylist is populated with because something like a null value will break it.

c0demonk3ya at 2007-7-28 17:15:38 > top of Java-index,Java Essentials,Java Programming...
# 2

how to rectify the problem

Can you send me the code?

rose@rosea at 2007-7-28 17:15:38 > top of Java-index,Java Essentials,Java Programming...
# 3

> how to rectify the problem

Step through your code with a debugger and check what your ArrayList is returning....

> Can you send me the code?

no

c0demonk3ya at 2007-7-28 17:15:38 > top of Java-index,Java Essentials,Java Programming...
# 4

NumberFormatException means that something is wrong with the String that you are trying to parse as a number - that its contents is not parsable as a integer. Try outputing each String you parse to console, and then see what Strings are causing exceptions.

boruthadzialica at 2007-7-28 17:15:38 > top of Java-index,Java Essentials,Java Programming...
# 5

@rose: what does the following display?

ArrayList list = getData();

Iterator i =list.iterator();

while(i.hasNext()){

Object o = (Object)i.next();

System.out.println(o.getClass());

}

prometheuzza at 2007-7-28 17:15:38 > top of Java-index,Java Essentials,Java Programming...
# 6

No thanks.

I solved the problem

Anyway thanks for you help

rose@rosea at 2007-7-28 17:15:38 > top of Java-index,Java Essentials,Java Programming...