0

I have been getting errors with this code my output displays in symbols I am using Java in netbeans gui builder

   String[] stringValues = jTextField1.getText().split("[,]");
        int[] numArray= new int[stringValues.length];

        for(int i=0; i<numArray.length; i++){
        numArray[i]= Integer.parseInt(stringValues[i]);
        }

        String value ="";
        for (int j = 0; j <= numArray.length; j++) {
        value += numArray[j];}
        jLabel1.setText( value.toString() );
6
  • what error do you get ? Could you add an exemple of input and output you get and output you expect ? Commented May 15, 2018 at 10:54
  • In your second for loop you write j <= numArray.length there you become a IndexOutOfBoundException. Write instead: j < numArray.length Commented May 15, 2018 at 10:58
  • hi i have been getting an output of @[2371 Commented May 15, 2018 at 11:01
  • 1
    i want to have a user input 1,2,3,4 and save it into an int array then have the array display in a label 1,2,3,4 Commented May 15, 2018 at 11:02
  • @Morchul i will try that now Commented May 15, 2018 at 11:02

2 Answers 2

2

Try out this:

String[] stringValues = jTextField1.getText().split(",");
//split the input "1,2,3,4" by "," result in array = ["1","2","3","4"]

int[] numArray= new int[stringValues.length];


for(int i=0; i<numArray.length; i++){
    numArray[i]= Integer.parseInt(stringValues[i]);
    //parse every string to int
}

String value ="";
for (int j = 0; j < numArray.length; j++) {
    value += numArray[j] + ",";
    //for each int in numArray ad: "numArray[j],"
}
//result is = "1,2,3,4,"

value = value.substring(0, value.length() - 1);
//remove last unused ","

jLabel1.setText( value );
Sign up to request clarification or add additional context in comments.

Comments

0

change your code from ->

        String[] stringValues = jTextField1.getText().split("[,]");
        int[] numArray= new int[stringValues.length];

        for(int i=0; i<numArray.length; i++){
          numArray[i]= Integer.parseInt(stringValues[i]);
        }

        String value ="";

        for (int j = 0; j <= numArray.length; j++) {
          value += numArray[j];
        }

        jLabel1.setText( value.toString() );

to this ->

    String[] stringValues = jTextField1.getText().split("[,]");
    int[] numArray= new int[stringValues.length];

    for(int i=0; i<numArray.length; i++){
       numArray[i]= Integer.parseInt(stringValues[i]);
    }

    String value = "";

    for (int j = 0; j < numArray.length; j++) {
      value += Integer.toString(numArray[j]) + ",";
    }

    jLabel1.setText(value);

9 Comments

Could you add explanations ? so the OP will understand why his code isn't and why your code is
added the explanation of cause of problem and answer as well.
I'm not sure he want to see the sum of the values. In his question he says: "in an integer array that later displays it in a label". So I think he want to see the integer array well formated like: 5,4,3,6. But I'm not sure maybe you are right.
@Kimberly check Morchul comment, that's why you get this error
yes, you are right @vincrichaud, and i have changed it in my code.
|

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.