almost there!! array search question


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: almost there!! array search question

  1. #1
    Join Date
    Jul 2004
    Posts
    92

    almost there!! array search question

    i have an array called clients full of object of my class Client. this array is created and given it's values in my BankDatabase class.

    to fill the array, i did this . . .
    import java.util.StringTokenizer;

    public class BankDatabase {
    private Client[] clients;
    private String adminPassword;

    public boolean readFile(String _dbFileName) throws FileNotFoundException, IOException
    {
    BufferedReader inFile = new BufferedReader (new FileReader (_dbFileName));
    StringTokenizer strAnalyzer = new StringTokenizer(inStr, ",");
    for (int i = 0; i<N; i++)
    {
    String inStr = inFile.readLine();
    // fill the fields with the record
    String firstName = strAnalyzer.nextToken();
    String lastName = strAnalyzer.nextToken();
    String ssn = strAnalyzer.nextToken();
    String accNumber = strAnalyzer.nextToken();
    byte accType = Byte.parseByte(strAnalyzer.nextToken());
    byte accStatus = Byte.parseByte(strAnalyzer.nextToken());
    float accAmount = Float.parseFloat(strAnalyzer.nextToken());
    String pin = strAnalyzer.nextToken();
    // create new Client object
    clients[i]= new Client(firstName, lastName, ssn, accNumber,
    accType, accStatus, accAmount, pin);
    }
    N is read outside the loop and is the number of array object (clients).

    i have made several methods that seem to be working ok, HOWEVER, i need to make a method that will give me the index number of a client give the client's account number. it returns -1 if that account number isn't found.

    here's what i have so far . . .
    public int searchForClient(String accNum)
    {
    boolean found = false;
    for(int i=0; i<clients.length; i++)
    {
    if(accNum==(clients[i].accNumber))
    {
    found = true;
    break;
    }
    if(found)
    return i;
    else
    return -1;
    }
    }

    the clients[i].accNumber part is giving me an error. i have tried also accNum which is what account number is refered to in the class Client. i have also tried to use .equals rather than the ==. i guess i might be running into problems because of scope. accNumber is refered to in the first method (the readFile) method where clients[] was created and from there it is passed into whatever, but maybe the method i am writing doesn't know what clients[i].accNumber means. i am ALMOST through with this assignment. just this one last thing to write. and i can't get my mind around what i am doing wrong.

    ALSO, we aren't supposed to declare ANY variables in our BankDatabase class that this method is in. all variables that we declare are supposed to be IN our methods.

    thanks
    crq

  2. #2
    Join Date
    May 2004
    Posts
    219
    You have to use .equals with Strings... if that isn't working then either accNumber isn't a string or it isn't public. It really helps if you tell us what the error is.

  3. #3
    Join Date
    Jul 2004
    Posts
    92
    i think i got it!! but the reason i say THINK is that all 4 of my classes compiled correctly, but i can't run the program. great. now i can't see if my methods and my class work.

    i don't know what to do with this error.

    ----jGRASP exec: java BankingSystem_hw7

    java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenizer.java:259)
    at BankDatabase.readFile(BankDatabase.java:26)
    at BankingSystem_hw7.main(BankingSystem_hw7.java:366)
    Exception in thread "main"
    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    any ideas would be great. thanks
    charity

  4. #4
    Join Date
    Jul 2004
    Location
    Netherlands
    Posts
    5
    It means you're trying to access a token from the tokenizer that is not there.

    Example:

    Code:
    String text = "segment1#segment2#segment3";
    StringTokenizer st = new StringTokenizer(text, "#");
    
    String segment1 = st.nextToken();
    String segment2 = st.nextToken();
    String segment3 = st.nextToken();
    //This is where you get the NoSuchElementException
    String segment4 = st.nextToken();
    You should use the hasMoreTokens() method of StringTokenizer to prevent this from happening

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    String[] lineParts = theLine.split(","); is far easier than stringtokenizer
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  6. #6
    Join Date
    Feb 2004
    Posts
    808
    String[] lineParts = theLine.split(","); is far easier than stringtokenizer
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

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