2

So I'm trying to make a fan java program for a game for a few of my friends, I'm trying to read the contents of a text file to store into an array/arraylist in the future but I'm unable to get string split working the way I hoped it would. I tried examples from this place that worked for people just to see if it will work but I get the same output.

importCards.java

BufferedReader in = null;
    try {
        in = new BufferedReader(new FileReader("dark.txt"));
        String read = null;
        while ((read = in.readLine()) != null) {
            read = in.readLine();
            String[] splited = read.split("||");
            for (String part : splited) {
                System.out.println(part);
            }
        }
    } catch (IOException e) {
        System.out.println("There was a problem: " + e);
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (Exception e) {
        }
    }

The text file is formatted as follows

17||Dark Soul Endor||Dark||2||1||Human||Main Characters||5|500000||833||126||78||23||Release of Spirit - Dark||Dissolve all Light Runestones to inflict Dark on all enemies||Power of Dark||Dark Attack x 150%

However when I tried and printed it I got this

1

8

|

|

D

a

r

k

and so on

3
  • You should try: read.split("\\|\\|"); Commented Oct 1, 2014 at 9:08
  • Use StringTokenizer then you can specify "||" as the token and get the required output Commented Oct 1, 2014 at 9:14
  • @Mobility I thought StringTokenizer used regular expressions also. Commented Oct 1, 2014 at 9:15

4 Answers 4

2

Regular expression have special tokens such as | which don't mean | literally, but in this case mean OR. i.e. in this case "" OR "" OR "" When you match by empty string it splits each character into it's own string.

What you intended was

String[] splited = read.split("\\|\\|");

You might be wondering, why two \\| and not \| The reason is that \ has a special meaning in Java and regular expressions. When you write \\| in Java it becomes "\|" as a string, i.e. two characters, which as a regular expression is | literally, instead of a special token.

BTW I suggest you use , or \t (tab) instead. This will not only be smaller but you will be able to edit the file in your favourite spread sheet editor such as Excel or LibreCalc. This makes it much easier to see where the columns and even add/remove a column or change their order.

Diablo II used , for its raw data files. ;)

If you read CSV or TSV files, there is libraries to make it easier to read/import such as OpenCSV's CSVReader

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

Comments

1

split() takes a regex, and | is a regex special char which means "or", so you're splitting by "empty string" or "empty string" or "empty string".

You need to escape them: "\\|\\|".

Comments

0

You need to scape || as \\|\\|

Eg:

String str = "17||Dark Soul Endor||Dark||2||1||Human||Main Characters||5|500000||833||126||78||23||Release of Spirit - Dark||Dissolve all Light Runestones to inflict Dark on all enemies||Power of Dark||Dark Attack x 150%";
String[] splited = str.split("\\|\\|");
for(String i:splited){
    System.out.println(i);
}

Comments

0

Remove read = in.readLine(); because you read 2 line in your example and change te split to:

String[] splited = read.split("\\|\\|");

becase || is a special char in regex.

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.