textField With array Question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 15 of 15

Thread: textField With array Question

Hybrid View

  1. #1
    Join Date
    Apr 2006
    Posts
    8

    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 08:05 AM.

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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.

  3. #3
    Join Date
    Apr 2006
    Posts
    8
    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

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You can trap for this condition - perhaps trim() and then if length() == 0. Return some other value or output your own error message.

  5. #5
    Join Date
    Oct 2005
    Location
    Plainsboro, NJ
    Posts
    12
    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.

  6. #6
    Join Date
    Apr 2006
    Posts
    8
    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

  7. #7
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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.

  8. #8
    Join Date
    Apr 2006
    Posts
    8
    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 08:58 AM.

  9. #9
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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 ...

  10. #10
    Join Date
    Apr 2006
    Posts
    8
    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.

  11. #11
    Join Date
    Apr 2006
    Posts
    8
    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?

  12. #12
    Join Date
    Apr 2006
    Posts
    8
    Ah I finally got it to work, thanks for all the help, it's been greatly appreciated!!

  13. #13
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You're welcome. Glad you got your program to work the way you imagined it.

Similar Threads

  1. array of arrays question
    By jim in forum Java
    Replies: 3
    Last Post: 10-19-2001, 09:24 AM
  2. accessing textfield elements in a array
    By dominic in forum Java
    Replies: 2
    Last Post: 04-30-2001, 03:27 AM
  3. casting array of array question???
    By Xasting in forum Java
    Replies: 1
    Last Post: 03-16-2001, 10:38 PM
  4. casting array of array question???
    By Xasting in forum Java
    Replies: 0
    Last Post: 03-15-2001, 05:08 PM
  5. Replies: 1
    Last Post: 08-18-2000, 12:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center