Dio Baccay
Dio Baccay

Reputation: 83

How to display an array in a textview in Java

I want to display an array in a text view, but it is not working. Please help me. Thanks.

I want to be the output looks this way...

example i will input 1,2,3,4,5 then..

output:
1
2
3
4
5

Here's my code:

String []values = ( input.getText().toString().split(","));
int[] convertedValues = new int[values.length];

for(int x=0;x<convertedValues.length;x++){
    convertedValues[x] = Integer.parseInt(values[x]);
    jLabel7.setText(Integer.toString(convertedValues[x]));
}

Upvotes: 0

Views: 288

Answers (4)

Husam
Husam

Reputation: 2079

After i see your update: You need to pass HTML tags format string inside the JLabel component to make new line:

String[] values = ( input.getText().toString().split(","));
String inLineValues = "";
for (String value : values) {
    inLineValues += value + "<br/>";
}
jLabel1.setText("<html>" + inLineValues + "</html>");

Upvotes: 1

TheKojuEffect
TheKojuEffect

Reputation: 21081

String[] values = input.getText().toString().split(",");
StringBuilder allValues = new StringBuilder();
for(String value: values)
    allValues.append(value);

jLabel7.setText(allValues);

All you're doing is removing "," from input.getText(), for that you can do

String text = input.getText.replace(",","");
jLabel7.setText(text);

Upvotes: 0

Husam
Husam

Reputation: 2079

You don't need the int[] convertedValues variable, Try This:

    String[] values = (input.getText().toString().split(","));
    for (String value : values) {
        jLabel7.setText(jLabel7.getText() + " " + value );
    }

Upvotes: 0

msangel
msangel

Reputation: 10362

Try this:

String []values = ( input.getText().toString().split(","));
int[] convertedValues = new int[values.length];
List<String> numbers = new ArrayList<String>();
for(int x=0;x<convertedValues.length;x++){
    convertedValues[x] = Integer.parseInt(values[x]);
    numbers.add(Integer.toString(convertedValues[x]));
}
jLabel7.setText(numbers.toString());

General idea is that you need to collect allvalues for output in one string, and than print it.

Upvotes: 0

Related Questions