Reputation: 799
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
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
Reputation: 33544
Try this,
for (int j =0; j < marks.size(); j++) {
analyzeTextArea.setText(j + marks.get(j)+"");
}
Upvotes: 1
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
Reputation: 117655
You need to do something like this:
analyzeTextArea.setText("" + (j + marks.get(j)));
Upvotes: 2