0

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));
1
  • the code seems to be incomplete, can you check that. Commented Oct 4, 2018 at 13:12

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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));
}

Comments

2

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.