Kamil Gosciminski
Kamil Gosciminski

Reputation: 17137

String newline split from file

a.txt :

1,2,3
1,2,6
2,4,5

Reading and splitting

String wynik = readFileAsString("a.txt");
String[] wyniki = wynik.split("");

I can't seem to find a way to split String out after newLines that are in file, and not specified like this (this is what I found on all other examples on stackoverflow):

1,2,3\n1,2,6\n2,4,5\n

My readFileAsString works correctly, and it actually reads it that way (without splitting):

1,2,31,2,62,4,5

I know I could add something to my file, to make it split by it, eg

1,2,3t
...

But I also need that file to be clear and without any unnecessary symbols.

Entire thing looks like this:

public static String readFileAsString(String file) {
    String line, results = "";
    try {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        // readLine() zwraca nulla na koncu pliku, stad zabezpieczenie
        while ((line = reader.readLine()) != null) {
            results += line;
        }
        reader.close();
        // return results;
    } catch (IOException e) {
        System.err.println("Error");
    }
    return results;
}

    public static void main(String args[]) {

        String wynik = readFileAsString("a.txt");
        System.out.println(wynik);
        String[] wyniki = wynik.split("[\\r\\n]+");

        ArrayList<Trojka> wynikArrList = new ArrayList<Trojka>();

        for (int i = 0; i < wyniki.length; i++) {
            String[] temp1 = wyniki[i].split(",");
            int m = 0;
            int n = 0;
            int t = 0;
            try {
                m = Integer.parseInt(temp1[0]);
            } catch (Exception e) {
            }
            try {
                n = Integer.parseInt(temp1[1]);
            } catch (Exception e) {
            }
            try {
                t = Integer.parseInt(temp1[2]);
            } catch (Exception e) {
            }

            Trojka temp = new Trojka(m, n, t);

            boolean newRecord = true;
            for (Trojka a : wynikArrList) {
                if (a.getM() == temp.getM() && a.getN() == temp.getN()) {
                    a.addT((int) temp.getT());
                    newRecord = false;
                    break;
                }
            }
            if (newRecord) {
                wynikArrList.add(temp);
            }
        }

        wynik = "";

        for (Trojka a : wynikArrList) {
            wynik += a.getM() + "," + a.getN() + "," + a.getT() + "\n";
        }

        System.out.println(wynik);  

    }
}

Upvotes: 2

Views: 4316

Answers (3)

rmuller
rmuller

Reputation: 12849

Be aware of the fact that a "new line" is represented by different characters, depending on the context. A save approach is to use split("\\R") (available in Java8+).

Example:

final String[] a = "one\ntwo\r\nthree\rfour\n".split("\\R");
assertArrayEquals(new String[]{"one", "two", "three", "four"}, a);

Also see https://stackoverflow.com/a/31060125/868941

Upvotes: 1

anubhava
anubhava

Reputation: 784898

If you want to split a string by newlines then try this regex:

String[] out2 = out.split("[\\r\\n]+");

Update: code for readFileAsString should be like this:

public static String readFileAsString(String file) {
   return new Scanner(new File(file)).useDelimiter("\\Z").next()'
}

Update 2:

public static String readFileAsString(List<String> Lines, String file) {
    String line, results = "";
    try {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        // readLine() zwraca nulla na koncu pliku, stad zabezpieczenie
        while ((line = reader.readLine()) != null) {
            results += line;
            lines.add(line);
        }
        reader.close();
        // return results;
    } catch (IOException e) {
        System.err.println("Error");
    }
    return results;
}

Now call it as:

ArrayList<Trojka> wynikArrList = new ArrayList<Trojka>();
String wynik = readFileAsString(wynikArrList, "a.txt");

Upvotes: 2

Salandur
Salandur

Reputation: 6453

You are trying to split your string with "", wich is the empty string. If you want to split it with by newline, use "\n" instead. But, since we do not see how you read the file (the read, you probably a returning a concatenated string fromreadFileAsString`, it is hard to tell where it is going wrong.

Upvotes: 0

Related Questions