-5

I have a text document that consists of a number of lines, each of which has a username, a comma, and a password. For example,

Joe, pass1
Sally, badpassword
Ahmed, goodpassword

And so forth.

I can read those lines into my program without any trouble. After that I want to separate them into an ArrayList. String.split() says it should separate them into an array. I need an ArrayList, and anyway the when I invoke split() my String is unchanged. So I don't know how to proceed.

This is where I think I am having a problem:

                while (true) {
                    if ((str = br1.readLine()) != null)
                        break;
                    out.println(str);
                    str.split(",");
                    userstr.add(str);
                }     

This is the whole class:

public BuggerGUI() {
    super("password");
    setLayout(new GridLayout(3, 4));
    userL = new JLabel("username");
    add(userL);
    userT = new JTextField(10);
    add(userT);
    passL = new JLabel("password: ");
    add(passL);
    passT = new JPasswordField(10);
    add(passT);
    blankL = new JLabel();
    add(blankL);
    loggin =  new JButton("login");
    add(loggin);
    handler Handler = new handler();
    userT.addActionListener(Handler);
    passT.addActionListener(Handler);
    loggin.addActionListener(Handler);
}

private class handler implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent event) {
        String user = userT.getText();
        String pass = passT.getText();
        if (event.getSource() == loggin) {
            out.println("username: " + user);
            out.println("password: " + pass);
            try {
                ArrayList userstr = new ArrayList();
                FileReader fr1 = new FileReader("C:\\Users\\Owner\\Desktop\\passlist.txt");
                BufferedReader br1 = new BufferedReader(fr1);
                String str;
                while (true) {
                    if ((str = br1.readLine()) != null)
                        break;
                    out.println(str);
                    str.split(",");
                    userstr.add(str);
                }     
                for (int i = 0; i < userstr.size();)
                    if (userT.equals(userstr.get(i)))
                        out.print("cool");   
            } catch(IOException e) {
                System.out.println(ERROR);
            }
        }
    }
}
John S.
  • 596
  • 1
  • 3
  • 14
Ethan Rork
  • 11
  • 6

4 Answers4

1

str.split(",") returns an array of strings that represent the pieces of the original string separated by ,. The original string is unaffected by this method. So in your code, this line is not doing anything because you are ignoring the return value.

What you probably want is:

String[] parts = str.split(",");
// do something with the parts

EDIT

If you want to store the parts of the string in an array list:

String[] parts = str.split(",");
List listOfParts = Arrays.asList(parts);

SECOND EDIT

If you are trying to populate the array list with just the list of usernames:

while((str = br1.readLine()) != null){
    out.println(str);
    String[] parts = str.split(",");
    if(parts.length > 0) {
        userstr.add(parts[0]);
    }
}

Then, you can simplify this piece of code:

for(int i = 0; i < userstr.size();){
    if(userT.equals(userstr.get(i))){
        out.print("cool");
    }
}

... by replacing it with this:

if(userstr.contains(user)) {
    out.print("cool");
}

Note that I compare against user which is a string, rather userT which appears to be a control.

Jason
  • 11,258
  • 3
  • 39
  • 46
1

If you uses "," as the argument to split(), your results might not be what you expect. If your string is "Sally, xyz", it will be split into "Sally" and " xyz". Note the space in the second string.

What you probably want is to use "\s*,\s*" which translates to "any number of whitespace characters followed by a comma followed by any number of whitespace characters".

FredK
  • 4,036
  • 1
  • 6
  • 11
0

I think you are looking for this:

String string = "this is the string to be split."; ArrayList arrayList = new ArrayList(Arrays.asList(string.split(" ")));

This will add the splitted strings to arrayList.

Jason
  • 11,258
  • 3
  • 39
  • 46
Anyssa K
  • 74
  • 9
-1

You used to have

str.split(",");

and split() is a void method. It doesn't alter str.

you could fix it by replacing str.split(",") with: str = str.split(","), or you could put it all on one line like:

while ((str = br1.readLine()) != null)
    userstr.add(str.split(",")[0]);
John S.
  • 596
  • 1
  • 3
  • 14
  • You can't replace `str.split(",")` with `str = str.split(",")` because str.split(",") returns an array of strings (and therefore is not a void method). – Jason Jan 22 '16 at 05:58
  • Also, the while loop breaks as soon as a line is read from br1. It should probably break when nothing further can be read from br1. – Jason Jan 22 '16 at 06:00