Java, Splitting an input file by colons

Go To StackoverFlow.com

5

I want to split some Strings in java on the colon character.

The format of the strings are: Account:Password.

I want to separate the tokens: Account and Password. What's the best way to do this?

2012-04-04 02:05
by user1304317
What's the specific issue - Paul Bellora 2012-04-04 02:06
I have a text file of USERNAME:PASSWORD , each Account/Password pair on its own line. I want to read the file line by line and put the username in its own variable and the password in its own variable - user1304317 2012-04-04 02:08
strLine.split(":") should give you username and pas - mshsayem 2012-04-04 02:08
Whats account and username? Both are different - sgowd 2012-04-04 02:08
Yes. Both are different and I want each to go to their own variable, go to the next line, do the same, and repeat - user1304317 2012-04-04 02:10
inside the loop: namepass = strLine.split(":"); name = namepass[0]; pass = namepass[1], shouldn't this do - mshsayem 2012-04-04 02:12


21

See Ernest Friedman-Hill's answer first.

String namepass[] = strLine.split(":"); 
String name = namepass[0]; 
String pass = namepass[1];
// do whatever you want with 'name' and 'pass'
2012-04-04 02:17
by mshsayem
Your answer helps alot! Would there be a way to make it read the first line of name:pass in a file, set those variables like above...then have it do some other tasks, THEN repeat the process, but with the next "name:pass" in the list - user1304317 2012-04-04 02:19
Inside the loop in your code, extract name and pass this way and do whatever you wan - mshsayem 2012-04-04 02:21
Oh okay got it! Also, random. But do you know how to add to a JList - user1304317 2012-04-04 02:22
http://www.exampledepot.com/egs/javax.swing/list_listaddrem.htm - mshsayem 2012-04-04 02:23


7

Not sure what part you need help with, but note that the split() call in the above will never return anything other than a single-element array, since readLine(), by definition, stops when it sees a \n character. split(":"), on the other hand, ought to be very handy for you...

2012-04-04 02:08
by Ernest Friedman-Hill
Changing it to split(":") just duplicates each entry in the list - user1304317 2012-04-04 02:10
Changing it to split(":") would give you the username and password as two separate array elements, at which point you're free to do whatever you want with them -- that being the goal of the whole enterprise as I understood it - Ernest Friedman-Hill 2012-04-04 02:12
Changing it to ":" just duplicates the entry in the list. So if I have 6 entries in my list, the output is 12 sets of "USERNAME:PASSWORD - user1304317 2012-04-04 02:15
I cannot imagine what it is we're talking about -- but it looks like @mshsayem's answer worked for you, so all's well that ends well - Ernest Friedman-Hill 2012-04-04 02:38


2

You need to use split(":"). Try this-

import java.util.ArrayList;
import java.util.List;

class Account {
    String username;
    String password;

    public Account(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

class Solution {
    public static void main(String[] args) {
        while(.....){//read till the end of the file
            String input = //each line
            List<Account> accountsList = new ArrayList<Account>();
            String splitValues[] = input.split(":");
            Account account = new Account(splitValues[0], splitValues[1]);
            accountsList.add(account);  
        }
        //perform your operations with accountList
    }
}

Hope it helps!

2012-04-04 02:12
by sgowd
That actually does get the ball rolling a bit! How would I put "sans" in one variable, and "pass" in another - user1304317 2012-04-04 02:14
Go through edited answer. I haven't used getters and setters, still you can use them - sgowd 2012-04-04 02:23
Ads