jTextField output value in array

92 views Asked by At

I want to save the value out of a TextField in an Array. But i think i do something wrong, may someone be so nice and could take a look on my code sniplet ?

 //Array

Carl[0]=jNumberFieldAE.getInt();
Carl[1]=jNumberFieldIT.getInt();
Carl[2]=jNumberFieldWIG.getInt();
Carl[3]=jNumberFieldPol.getInt();
Carl[4]=jNumberFieldRel.getInt();
Carl[5]=jNumberFieldE.getInt();
Carl[6]=jNumberFieldDeutsch.getInt();
Carl[7]=jTextFieldName.getText();

Is .getText(); not right ?

2

There are 2 answers

1
Hakim On

JTextField has String as value, so you must parse it to retreive your integer value. Try this code :

Carl[0]= Integer.parseInt(jNumberFieldAE.getText());
...
0
Sheetal Mohan Sharma On

Array are fixed length, Size of array cannot be modified and cant have different type. Make it an arraylist and add dynamically.

List<String> ArrList= new ArrayList<String>()

name = jTextFieldName.getText();
text1= Integer.parseInt(jNumberFieldAE.getText());
...more

// Add to list
ArrList.add(name);
ArrList.add(text1);

If you still need array convert List to Array

String [] strArr = ArrList.toArray(new String[ArrList.size()]);