0

I have two arrays of JTextField and I want to store it into another array.

JTextField[] proText, atText; 


int[] burst, arrive;

I have tried the usual way of the passing the value of the array

while(true){
    if(atText[lowerBound].getText() != " " && proText[lowerBound].getText() != " "){
        bt = proText[lowerBound].getText();
        at = atText[lowerBound].getText();
        burst[lowerBound] = Integer.parseInt(bt);
        arrive[lowerBound] =Integer.parseInt(at);
        break;
    }else 
        break;
}

***the 'lowerBound' is being incremented when I click the button.

BTW this is dynamic, in which it adds another text field when I am clicking the button. The problem here is that only the last number is displayed and the rest are zero. Is there another way to get the value of a text field?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
knhrvs
  • 3
  • 3
  • I don't understand what do you want to do? but first if you're comparing strings use equals and not == – b.GHILAS Jan 28 '20 at 13:39
  • it will check if the textfield is null or not – knhrvs Jan 28 '20 at 13:41
  • if you want to check for null, use getText() == null, if you want to check for blank use getText().trim().isEmpty() but don't use == for strings – b.GHILAS Jan 28 '20 at 13:44
  • yes, thank for that. But is there still another way to get the value of JTextField and store it in int array? – knhrvs Jan 28 '20 at 13:46
  • Using getText is the way to get the text of JTextField. What exactly the while loop do ? – b.GHILAS Jan 28 '20 at 13:50
  • because the JTextField is an array, it is incharge of iterating for the next Jtextfield – knhrvs Jan 28 '20 at 13:54
  • I understand that you want to save each jtextfield text in the array, but when the loop are executed, and what's the point of a loop if you break from it after only one iteration ?? just post all of your code and add some clarifications – b.GHILAS Jan 28 '20 at 13:57
  • I Have changed some somethings, you can take a look if you like. this is the link : https://justpaste.it/4jew2 – knhrvs Jan 28 '20 at 14:15
  • Just to be sure, you create text fields dynamically and you want to store each value in an array. What's the total number of dynamic fields you'll create ?? – b.GHILAS Jan 28 '20 at 14:27
  • That depends on the user, as long as they click the add button – knhrvs Jan 28 '20 at 14:31

1 Answers1

0

I read your code, the problem is simple, each time you hit one of the buttons, you re-init your arrays. Of cours the previous values will be lost.

A quick fix is to copy the values first and then store the new value in your actionPerformed method

int[] atTemp = new int[arrsize];
int[] btTemp = new int[arrsize];
for (int i = 0; i < arrsize - 1; i++) {
    atTemp[i] = at[i];
    btTemp[i] = bt[i];
}
at = atTemp;
bt = btTemp;

But if you don't know the size of your arrays then use ArrayList, then you don't have to re-init your array each time you hit the buttons.

b.GHILAS
  • 2,273
  • 1
  • 8
  • 16
  • Hii Thank You for your help, i got the answer to my problem. Even though it took me awhile to realize that there is a more efficient way to solve. Thank you. Thank you for mentioning arraylist that really helped alot. – knhrvs Jan 28 '20 at 17:10