-
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
Last edited by donce; 04-15-2006 at 07:05 AM.
-
Other than using "else if" and "else" rather than all of those following "if's" - so only one response to one event is possible - this is as good as it gets. This is the way we have to approach event handling.
-
Ok thanks, just one more thing though, if there is nothing entered in the textField and a button is pressed a horrible looking error message appears, is there any way of getting round this so that, say, if nothing is entered and a button is pressed the number of the index is then displayed in the textFiled when rather than getting that error message??
Cheers
-
You can trap for this condition - perhaps trim() and then if length() == 0. Return some other value or output your own error message.
-
Use a Try{}Catch{} statement. Write the code to display the index in the catch.
Also, wouldn't name.length - name.length always be 0? Unless I'm wrong, you could shorten the code slightly and reduce the risk of error by saying if ( e.getSource() == previous && currentEntry > 0) instead.
-
I'm not sure where this code should go or how to set it up, would it be something like
try{ textString = "";
}
catch (NumberFormatException errorObject) {
num = currentEntry;
textField.setText(textString = Integer.toString(currentEntry));
}
?? or would i have to put the try{} round the if statements
-
This error is coming because of your first two lines of code. It sounds like you have not put this test for valid input before you set the value of num.
-
Code:
boolean found = false;
int index = 0;
if ( e.getSource() == find ) {
while((found == false) && (index < name.length)) {
if ( name[index].equalsIgnoreCase(textString)) {
found = true;
currentEntry = index;
textField.setText(textString = Integer.toString(currentEntry));
}
else {
index++;
}
if((found == false) && (index == name.length)) {
num = currentEntry;
textField.setText(textString = Integer.toString(currentEntry));
}
}
}
I now have the above code that searches for a name that is located in an array [name] form a textField, if it is found it displays the relative information, if not the current information is kept displayed. This works all fine and well but now I'm wondering how to allow it to display the information of a certain person if only part of the name is entered into the textField by the user
Exmple
If there is a index in the name array that has the name Michael for example, I want to be able to allow the user to enter 'mich' for example, or even just 'm' and then the relavent details for michael to be displayed when the find button is clicked.
I thought mayb using substring or indexOf??? but I'm not sure if this would work or how I would go about writing it, any ideas???
Thanks
Last edited by donce; 04-18-2006 at 07:58 AM.
-
You're introducing complexity to your system, but it can be manageable.
Look at the String class's "contains" method. Will you then display all entries which match, and then allow choice from that display ...
-
Yeah I had a look at that but just won't work, I want it so that no matter the case of the letter (upper or lower) then it will still work. I'm really out of ideas.
-
Well I managed to use .contains to work but is there a way so that it will work no matter what case the the letters are in?? maybe using to LowerCase before checking if it contains it?
-
Ah I finally got it to work, thanks for all the help, it's been greatly appreciated!!
-
You're welcome. Glad you got your program to work the way you imagined it.
Similar Threads
-
Replies: 3
Last Post: 10-19-2001, 08:24 AM
-
Replies: 2
Last Post: 04-30-2001, 02:27 AM
-
Replies: 1
Last Post: 03-16-2001, 09:38 PM
-
Replies: 0
Last Post: 03-15-2001, 04:08 PM
-
Replies: 1
Last Post: 08-18-2000, 11:29 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks