9

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?

5
  • 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. Commented Apr 4, 2012 at 2:08
  • 1
    strLine.split(":") should give you username and pass Commented Apr 4, 2012 at 2:08
  • Whats account and username? Both are different? Commented Apr 4, 2012 at 2: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. Commented Apr 4, 2012 at 2:10
  • 1
    inside the loop: namepass = strLine.split(":"); name = namepass[0]; pass = namepass[1], shouldn't this do? Commented Apr 4, 2012 at 2:12

3 Answers 3

29

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'
Sign up to request clarification or add additional context in comments.

3 Comments

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?
Inside the loop in your code, extract name and pass this way and do whatever you want
Oh okay got it! Also, random. But do you know how to add to a JList?
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...

4 Comments

Changing it to split(":") just duplicates each entry in the list.
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.
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"
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.
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!

2 Comments

That actually does get the ball rolling a bit! How would I put "sans" in one variable, and "pass" in another?
Go through edited answer. I haven't used getters and setters, still you can use them.

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.