1

I have a file with text in this format:

text:text2:text3
text4:text5:text6
text7:text8:text9

Now what I want to do, is to read the first line, separate the words at the ":", and save the 3 strings into different variables. those variables are then used as parameter for a method, before having the program read the next line and doing the same thing over and over again.. So far I've got this:

public static void main(String[] args) {

BufferedReader reader = null;

try {
    File file = new File("C://Users//Patrick//Desktop//textfile.txt");
    reader = new BufferedReader(new FileReader(file));

    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }

} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Also, I've tried this for separation (although not sure Array is the best option:

String[] strArr = sCurrentLine.split("\\:");
Opsse
  • 1,851
  • 2
  • 22
  • 38
Patrick
  • 31
  • 2

1 Answers1

1

Use String[] parts = line.split(":"); to get an array with text, text2 etc. You can then loop through parts and call the method you want with each item in the list.

Your original split does not work, because : is not a special character in Regex. You only have to use an escape character when the split you are trying to achieve uses a special character.

More information here.

DjangoBlockchain
  • 534
  • 2
  • 17