Java read lines from file to array

Discussion in 'Programming' started by Silver89, Apr 5, 2011.

  1. #1
    Is it possible to read lines from a .txt file in Java to an array and then search the array for possible values?

    So for example a file containing user logons to see if a username and password is correct? (I know it's not secure in any way).
     
    Silver89, Apr 5, 2011 IP
  2. ViolentAJ

    ViolentAJ Well-Known Member

    Messages:
    581
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    128
    #2
    Yes, it is. You can read text from a file, and add strings to an array. It all depends on how the text file is formatted.

    Once you have the strings, there are a variety of ways that you can check. It depends on the system you are using.
     
    ViolentAJ, Apr 6, 2011 IP
  3. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #3
    If I have a file consisting of:

    [Fred, 1, 1989]
    [Barry, 6, 1967]
    [Bob, 9, 2001]

    How can I get just the names?
     
    Silver89, Apr 6, 2011 IP
  4. QiSoftware

    QiSoftware Well-Known Member

    Messages:
    805
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    158
    #4
    search through the first comma in each line or however the data is delimited for each person-- and save the results to a string.

    Q..
     
    QiSoftware, Apr 6, 2011 IP
  5. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #5
    But I'm struggling to get the code for the above working, could you give a brief example?
     
    Silver89, Apr 6, 2011 IP
  6. QiSoftware

    QiSoftware Well-Known Member

    Messages:
    805
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    158
    #6
    look into the indexOf function within java.

    Q...
     
    QiSoftware, Apr 6, 2011 IP
  7. mubashirnisar

    mubashirnisar Banned

    Messages:
    2,186
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    0
    #7
    read the file till the \n and store it to the array.
     
    mubashirnisar, Apr 8, 2011 IP
  8. opx

    opx Greenhorn

    Messages:
    44
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #8
    Read by line, add to an ArrayList, and then use .toArray() :)

    - opx
     
    opx, Apr 8, 2011 IP