2

The single entry of my ArrayList is of the form:

public class Account {
    String username;
    String password;
}

I managed to put some "Accounts" in the a text file, but now I don't know how to read them. This is how my ArrayList looks in the text file:

username1 password1 | username2 password2 | etc

This is a part of the code I came up with, but it doesn't work.

public static void RdAc(String args[]) {

    ArrayList<Account> peoplelist = new ArrayList<Account>(50);

    int i,i2,i3;
    String[] theword = null;

    try {

        FileReader fr = new FileReader("myfile.txt");
        BufferedReader br = new BufferedReader(fr);
        String line = "";

        while ((line = br.readLine()) != null) {
            String[] theline = line.split(" | "); 

            for (i = 0; i < theline.length; i++) {
                theword = theline[i].split("  "); 
            }

            for(i3=0;i3<theline.length;i3++)  { 
                Account people = new Account();

                for (i2 = 0; i2 < theword.length; i2++) {

                    people.username = theword[i2];
                    people.password = theword[i2+1];
                    peoplelist.add(people);
                }  
            } 

        }
    }
    catch (IOException ex) {
        System.out.println("Could not read from file");
    }
Lox
  • 299
  • 2
  • 4
  • 18
  • 1
    Almost exactly the same thing was asked 2 days ago: http://stackoverflow.com/questions/2788080/reading-a-text-file-in-java Your case is a little bit more complicated, but those answers will still be useful for you. – Jonik May 09 '10 at 21:17
  • In the future, please take more care with your code formatting. It took more time than it should have to reformat it into something readable. – Michael Petrotta May 09 '10 at 21:17
  • Hmm, seems like some sort of sequel to this: http://stackoverflow.com/questions/2777107/how-to-read-data-from-file-into-array-java – Jonik May 09 '10 at 21:22
  • things didn't quite worked out the way they should have, so i had to ask again. sorry for the inconvenience – Lox May 09 '10 at 21:26
  • Jonik: Please don't edit questions just to change the number of spaces in the indent. 4 is pretty standard for Java – RichH May 09 '10 at 21:36
  • @Michael: if reformatting his code took more time than it should have, maybe next time pipe it through astyle (http://astyle.sf.net/) and let it do indentation for you. – Ken Bloom May 09 '10 at 21:37
  • @Ken - thanks for the link. This question actually led me to explore the reformatting capabilities of Notepad++, which does a fine job. Still shouldn't have to download and use a new tool to render a question readable. – Michael Petrotta May 09 '10 at 21:42
  • RdAc? Is there a letter shortage? – Skip Head May 09 '10 at 23:23
  • 1
    @RichH: I did no such thing (on purpose anyway). I was only editing tags; page ended screwed up; had to fix it somehow. Note that on SO history does not show every change, and edit conflicts are more problematic than on some other systems. – Jonik May 10 '10 at 14:18

3 Answers3

3

a more robust solution would be to define a regular expression that matches the line and use the Matcher.group(...) call to pull out the fields. for example,

String s = 
Pattern p = Pattern.compile("\\s*(\\w+)\\s+(\\w+)\\s+");
String line;
while ((line = br.readLine()) != null) {
  Matcher m = p.match(line);
  while (m.find()) {
    String uname = m.group(1);
    String pw = m.group(2);
... etc ...

this is also a lot more robust when it comes to dealing with format problems. all it does it look for pairs of words. it doesn't care what string is used to separate them or how many spaces there are in between.

i just guessed at the regex. you will probably need to tune it a bit depending on the exact format of the input.

Jeff
  • 145
  • 1
2

It's not clear what's wrong from your question. However, I'd expect you to process the results of splitting on " " (theword) within the containing loop, rather than processing it outside.

      for (i = 0; i < theline.length; i++) {
               theword = theline[i].split("  "); 
               Account people = new Account();
               for (i2 = 0; i2 < theword.length; i2++) {
                    people.username = theword[i2];
                    people.password = theword[i2+1];
                    peoplelist.add(people);
               }  
          }
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • after i run the program, this i what i get: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at this line: people.username = theword[i2]; and if i run it again it says at this line: people.password = theword[i2+1]; – Lox May 09 '10 at 21:34
1

What does it do wrong? Have you stepped through with a debugger? If so, which part is causuing the issue?

Things that I've noticed:

  1. You i2 loop should be for (i2 = 0; i2 < theword.length; i2 += 2) {
  2. I wouldn't set the initial size of the ArrayList unless you know how many items are in the file.
  3. Are there two spaces between your username and password?
  4. Have you looked into serialization?
  5. Why not have a new line for each username and password It would be a lot easier to load.

    username1
    password1
    username2
    password2

RichH
  • 6,108
  • 1
  • 37
  • 61