0

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]));
}
4
  • 1
    What's the point of creating int[] convertedValues? And also share the error message you get. Commented Sep 25, 2013 at 15:54
  • 1
    It seems you are using the same jLabel to display the values of the array. It doesn't make sense. Commented Sep 25, 2013 at 15:56
  • it only displays my last input Commented Sep 25, 2013 at 15:56
  • @Dio Baccay because you are using in an for loop, thats why its displaying the last value. Commented Sep 25, 2013 at 15:57

4 Answers 4

1

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>");
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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 );
    }

Comments

0
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);

2 Comments

You create as much Objects as elements in the String array. That's not very effective.
@SteveBenett I was just providing solution. Thanks for pointing out, Now It's using StringBuilder. Any more improvements?

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.