textField With array Question
Hi I have the following code:
textString = textField.getText();
num = Integer.parseInt(textString);
if ( num >= name.length-name.length && num <= name.length-1) {
if ( e.getSource() == first ) {
currentEntry = name.length - name.length;
textField.setText(textString = Integer.toString(currentEntry));
}
if ( e.getSource() == previous && currentEntry > name.length - name.length ) {
currentEntry--;
textField.setText(textString = Integer.toString(currentEntry));
}
if ( e.getSource() == next && currentEntry < name.length-1){
currentEntry++;
textField.setText(textString = Integer.toString(currentEntry));
}
if ( e.getSource() == last ) {
currentEntry = name.length-1;
textField.setText(textString = Integer.toString(currentEntry));
}
if ( e.getSource() == goTo ) {
currentEntry = num;
textField.setText(textString = Integer.toString(currentEntry));
}
}
else {
if ( e.getSource() == first ) {
currentEntry = name.length - name.length;
textField.setText(textString = Integer.toString(currentEntry));
}
if ( e.getSource() == previous && currentEntry > name.length - name.length ) {
currentEntry--;
textField.setText(textString = Integer.toString(currentEntry));
}
if ( e.getSource() == next && currentEntry < name.length-1 ) {
currentEntry++;
textField.setText(textString = Integer.toString(currentEntry));
}
if ( e.getSource() == last ) {
currentEntry = name.length-1;
textField.setText(textString = Integer.toString(currentEntry));
}
if ( e.getSource() == goTo ) {
num = currentEntry;
textField.setText(textString = Integer.toString(currentEntry));
}
}
Basically as you can see there are 5 buttons and a textField. The code given here does work, it starts off with a zero in the text field which is the number of the first index in the array name[], each index in the array stores some personal details which is shown on screen. So when the 'last' button is pressed the number in the textfield should now contain the last index number in the array, here for example this would be 4, and the information of that index is displayed on screen. The 'first', 'next' and 'previous' buttons are pretty self explanatory in what they do. Also the 'goTo' button when pressed displays the information of that particular index number that has been entered into the textField.
Also when the 'previous' button is pressed and the information deisplayed is of the first index nothing happens, and similarly with the 'next' button and the last index. Also if the number entered into the textField is not an index number, either too high, or too low (giving a negitive number), nothing happens and the current information is still displayed with the correct number in the textField displayed appropriatley.
All I am wondering is, is there a way to achieve all of this, but with a smaller amount of code, i.e without so many if statements?
Thanks