user10334384
user10334384

Reputation:

Integer cannot be applied error to java.lang.string

I'm sure this is a silly problem but I'm new to java. Can anyone see the possible cause of this problem?

ArrayList<String> timesTableContent = new ArrayList<>();

    for (int i = 1; i <= 10; i++) {

        timesTableContent.add(Integer.toString(timesTable + "      X     " + i + "      =     " + i * timesTable));

Upvotes: 0

Views: 775

Answers (4)

Balaji S B
Balaji S B

Reputation: 36

Instead of Integer.toString() try to concatenate integer using String.valueOf()

Instead of

ArrayList<String> timesTableContent = new ArrayList<>();

for (int i = 1; i <= 10; i++) {

    timesTableContent.add(Integer.toString(timesTable + "      X     " + i + "      =     " + i * timesTable));

Replace this

ArrayList<String> timesTableContent = new ArrayList<>();

for (int i = 1; i <= 10; i++) {
    timesTableContent.add(timesTable + "      X     " + String.valueOf(i) + "      =     " + String.valueOf(i * timesTable)));
}

But instead of use this

Just concatenate without convert it. Thanks

Upvotes: 0

odur37
odur37

Reputation: 41

EDIT: You are calling Integer.toString() on a String, when the method only accepts an Integer. This is why you get the error message you provided.

I believe you have misplaced your parenthesis a little bit.

You could build the string you want with:

ArrayList<String> timesTableContent = new ArrayList<>();

int timesTable = 2;

for (int i = 1; i <= 10; i++) {
    timesTableContent.add(timesTable + 
                          "      X     " + 
                                       i + 
                          "      =     " + 
                          (i * timesTable));
}

Upvotes: 3

Robo Mop
Robo Mop

Reputation: 3553

Integer.toString() converts an Integer Object to String.

timesTable + " X " + i + " = " + i * timesTable returns a String value by itself. So you can directly add this to your code as follows:

ArrayList<String> timesTableContent = new ArrayList<>();

for (int i = 1; i <= 10; i++) {

    timesTableContent.add(timesTable + "      X     " + i + "      =     " + i * timesTable);

In your code, you are passing a String Object in a method that actually accepts an Integer value. That's the whole problem.

Upvotes: 3

Amila
Amila

Reputation: 5213

Why do you use Integer.toString call? Try removing it.

Upvotes: 2

Related Questions