Question

This code seems to be broken but I can't tell why:

System.out.println(line);
// prints: Some Name;1|IN03PLF;IN02SDI;IN03MAP;IN02SDA;IN01ARC

String args[]            = line.split("|");
String candidatArgs[]    = args[0].split(";");
String inscrieriString[] = args[1].split(";");


System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(candidatArgs);
System.out.println("[0]:" + candidatArgs[0]);
System.out.println("[1]:" + candidatArgs[1]);

// prints: S
//         [Ljava.lang.String;@4f77e2b6
//         [0]:

I have no idea why that happens. By my logic:

String args[]            = line.split("|");
[0]: Some Name;1
[1]: IN02SDI;IN03MAP;IN02SDA;IN01ARC

Instead of:

[0]: S

In case you'd like more code: This should compile even if it doesn't do much (removed as much un-necessary code as I could)

Main:

Have a file: Candidati.txt

containing: Some Name;1|IN03PLF;IN02SDI;IN03MAP;IN02SDA;IN01ARC

import java.util.ArrayList;

Repository repository = new Repository ("Candidati.txt"); // file name

    ArrayList<Candidat> candidati = repository.getCandidati();
    System.out.println(candidati);

Repository

import java.util.ArrayList;

public class Repository {

    private String fisierCuCandidati;

    private ArrayList<Candidat> listaCandidati;

    public Repository (String fisierCuCandidati) {

        this.fisierCuCandidati = fisierCuCandidati; // file name

        this.listaCandidati = new ArrayList<Candidat>();

        this.incarcaCandidati();
    }


    public void incarcaCandidati() {
        FileReader in = null;
        BufferedReader input = null;

        //try {


            in = new FileReader (this.fisierCuCandidati);
            input = new BufferedReader (in);

            String line;
            while ((line = input.readLine()) != null) {

                System.out.println(line);

                String args[]            = line.split("|");
                String candidatArgs[]    = args[0].split(";");
                String inscrieriString[] = args[1].split(";");


                System.out.println(args[0]);
                System.out.println(args[1]);
                System.out.println(candidatArgs);
                System.out.println("[0]:" + candidatArgs[0]);
                System.out.println("[1]:" + candidatArgs[1]);
            }


}

Candidat

public class Candidat {

    public Candidat (String nume) {

    }

    public Candidat (String nume, int id) {

    }
Was it helpful?

Solution

String.split uses a regular expression so you need to escape the pipe |, which is a special character (meaning OR):

String args[] = line.split("\\|");

Also to print the String array output rather the Object.toString representation, you will need:

System.out.println(Arrays.toString(candidatArgs));

OTHER TIPS

You either need to escape the pipe or use it inside a character class in your split, as String#split takes a regex, and | is a meta character in regex. So, use this instead:

String args[] = line.split("\\|");

or:

String args[] = line.split("[|]");

The reason a character class works is because, inside a character class, te meta-characters have no special meaning. So, a pipe is just a pipe, and not an alternation character.

In addition to that, you should use Arrays#toString method to print your array.

change to:

String args[]            = line.split("\\|");

your | won't work because the parameter of split is a regex, | has special meaning(or) in regex.

| is a special character in java regexes, so you need to escape it like \\|. I generally do

line.split(Pattern.quote(separator))

where Pattern is http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html, and separator is whatever separator you use. This automatically takes care of escaping special characters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top