user1419306
user1419306

Reputation: 799

How to display an array of integers in a JTextArea?

for (int j =0; j < marks.size(); j++) {
    analyzeTextArea.setText(j + marks.get(j));
}

The above code gives me the following error:

required: java.lang.String found: int

Upvotes: 1

Views: 2447

Answers (5)

user6265568
user6265568

Reputation: 11

That should work but instead of .setText(), you should use .append(). because .setText() deletes the previous contents and writes it. but .append() just adds on information

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

Try this,

for (int j =0; j < marks.size(); j++) {
    analyzeTextArea.setText(j + marks.get(j)+"");
    }

Upvotes: 1

alain.janinm
alain.janinm

Reputation: 20065

I guess marks.get(j) give you an Integer. So when you do j + marks.get(j) you add the value of marks.get(j) to the value of j.

So you end with an Integer as result of j + marks.get(j). But setText expect a String.

You have several possibilities now depending on you needs.

analyzeTextArea.setText(Integer.toString(j + marks.get(j)));

This case still make the addition then convert it to String in order to respect setText parameter type.

With this :

analyzeTextArea.setText("" + (j + marks.get(j)));

"" tells that the parameter will be a String and then you will concatenate j and marks.get(j). So, for example, for the first loop you will have something that start with 0

Now using setText in a loop don't really make sense because only the last value set in the loop will be used you probably should use JTextArea#append(String).

Upvotes: 3

Eng.Fouad
Eng.Fouad

Reputation: 117655

You need to do something like this:

analyzeTextArea.setText("" + (j + marks.get(j)));

Upvotes: 2

ollins
ollins

Reputation: 1849

analyzeTextArea.setText(Integer.toString(j + marks.get(j)));

Upvotes: 1

Related Questions