-6

Is there an equivalent for getString() that works for integers?

I have some integer values passing into a function and eventually placing in a SQLite table.

x.getString() //works for string values

works for string values, but doesn't work for integer values.

Below is my code:

Employee createdEmployee = employeeDAO.createEmployee(
    address.toString(),
    phoneNumber.toString(),
    salary //this is an integer value and getting error
);
Asif Mujteba
  • 4,596
  • 2
  • 22
  • 38
  • Use `toString()` or use `""+ salary` – K Neeraj Lal May 25 '15 at 07:30
  • 1
    Please do a research before asking a question. This has already been asked here: http://stackoverflow.com/questions/5071040/java-convert-integer-to-string and http://stackoverflow.com/questions/4105331/how-to-convert-from-int-to-string and http://stackoverflow.com/questions/7839642/android-int-to-string – unrealsoul007 May 25 '15 at 07:38
  • I figured it out. This is what I was looking for. int sal = Integer.valueOf(salary.toString()); – Norrin Radd May 25 '15 at 13:05

2 Answers2

2

You can insert salary as a string but whenever you want to use salary as a integer.. then you can use

Integer.parseInt(salary);
1

There are 3 options:

1- Use helper method defined in String class to convert various objects including int to string:

Employee createdEmployee = employeeDAO.createEmployee(
                address.toString(),
                phoneNumber.toString(),
                String.valueOf(salary)
        );

2- Simply append an empty string to the int which would implicitly convert int to string:

Employee createdEmployee = employeeDAO.createEmployee(
                address.toString(),
                phoneNumber.toString(),
                salary + ""
        );

3- Use helper method defined in Integer class to string to int:

Employee createdEmployee = employeeDAO.createEmployee(
                address.toString(),
                phoneNumber.toString(),
                Integer.parseInt(salary)
        );
Asif Mujteba
  • 4,596
  • 2
  • 22
  • 38