简体   繁体   中英

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

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) .

您需要执行以下操作:

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

Try this,

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM