0

Hi the problem in the code is at the end finalResult[index]= mark; code, mark gives error. It says cannot convert int to string. How can i fix it ?

    System.out.println("Please choose a criteria (2-7) ?");
    topic = in.nextInt();
    System.out.println("Please enter a mark :");
    int mark = in.nextInt();
    final int size = cols.length;
    String[] finalResult = new String[size];
    int index = 0;

    while(index<finalResult.length ) {
        if (index==topic) {
          finalResult[index]= mark;
        } else {
        finalResult[index]=cols[index];
        }
        index++; 

    }
    }
1
  • try something like finalResult[index]= mark + ""; Commented Apr 26, 2020 at 14:44

3 Answers 3

2

The problem is here:

finalResult[index] = mark;

You can't put int number to String array:

Error:
incompatible types: int cannot be converted to java.lang.String

You need to convert number to String before adding to the array.

You have to change as follows:

finalResult[index] = String.valueOf(mark);

or

finalResult[index] = mark + "";
Sign up to request clarification or add additional context in comments.

Comments

1

Casting integer to string is done with method String.valueOf so in your case it would be

String.valueOf(mark)

1 Comment

Index is an int.
0

Change the mark declaration in the code as below,

String mark = in.nextLine();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.