-2
if(Integer.parseInt(n)==(m[i].age))
            {
                m[i].showdetails();
            }

I am getting number format exception in if condition In this code n is an string and m[i].age is an integer and I want to compare them

thank you:)any help would be appreciated.

1 Answers1

-1

dont try to convert string to integer instead convert integer to string

if(n.compareTo(Integer.toString(m[i].age))==0)
            {
                m[i].showdetails();
            }
Saurabh Gupta
  • 122
  • 2
  • 12
  • Thank you for the answer. Its working :) – Suraj Chauhan Jun 17 '17 at 11:51
  • 1
    That is one approach - but be aware that this way you'll just get `false` as a result for any `n` that is not exactly a valid representation of the given integer - you might miss bugs that way. Also, I'd prefer `n.equals(Integer.toString(m[i].age))` – Hulk Jun 17 '17 at 11:54
  • @SurajChauhan This is one approach, but it's wrong in my opinion. Always solve your bugs, **never** do hacks like this one to avoid solving your bugs. – BackSlash Jun 17 '17 at 12:25