Results 1 to 6 of 6
  1. #1
    rayda is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default read from text file

    hi there!
    I have a file.txt looks like this:
    Michael
    001
    Yeeka
    002
    Sam
    003
    Tom
    004
    Michael is the member name, while 001 is the ID number.
    How can I write a coding to validate that whether user has type in the correct name and ID that is inside the file.txt?
    such as, I have a coding that ask user to enter name and ID.
    if user type "Michael" and "001", how can I have a coding that can validate user is a member?
    please help!
    thanks a lot!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I would read the file into a HashMap<String, String> with the name String being the value (so you can have duplicates, and the number String (and I'd use a String since you're not using this data in a numeric way). Then when you get the user sign-on, just see if the id is in the hashmap and has the correct corresponding name value.

  3. #3
    rayda is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    Sorry..
    but can you show me how to do that?..
    I am still new to Java..
    Thanks

  4. #4
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    I'm sorry, but I'm having a hard time comprehending your question.
    I think you want to make a program where the user inputs a name and an id, then it checks to see if the name and id is in a file.
    If so, I suggest you take a look at the Map interface and the classes which implement that.
    You can read through the file and add the id and name of a person, then query the map to see if the information is correct.

    Java Code:
    while(info in file) {
                myMap.put(id, name);
            }
            // get user id and name
            if(myMap.containsKey(userId))
                if(myMap.get(userId).equals(userName))
                    // is in file (validated)

  5. #5
    rayda is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    Mr.Beans, is there other simple way to do so other than using Map?

  6. #6
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    I wouldn't call it easier. However, an alternative (yet not suggested way) would be to loop get the id and name, then compare it to each line in the list each time you get the input.
    Java Code:
    boolean valid = false;
            String name, id;
            // get input
            while(lines in text) {
                String tmpId, tmpName;
                // get the next two lines in file
                if(tmpId.equals(id) && tmpName.equals(name))
                    valid = true;
            }
    I suggest you go with the map however. It will help you out in the long run.

Similar Threads

  1. Replies: 2
    Last Post: 01-26-2009, 04:28 PM
  2. Read and modify text file
    By heartysnowy in forum New To Java
    Replies: 27
    Last Post: 11-19-2008, 10:03 AM
  3. [SOLVED] How do I read from a text file
    By matzahboy in forum New To Java
    Replies: 5
    Last Post: 11-17-2008, 04:47 AM
  4. Replies: 40
    Last Post: 07-04-2008, 08:41 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 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
  •