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");
    }
9
  • 1
    Almost exactly the same thing was asked 2 days ago: 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. Commented May 9, 2010 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. Commented May 9, 2010 at 21:17
  • Hmm, seems like some sort of sequel to this: stackoverflow.com/questions/2777107/… Commented May 9, 2010 at 21:22
  • things didn't quite worked out the way they should have, so i had to ask again. sorry for the inconvenience Commented May 9, 2010 at 21:26
  • 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. Commented May 10, 2010 at 14:18

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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);
               }  
          }

1 Comment

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];
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.