mkunkel mkunkel - 1 month ago 14
Java Question

Java Scanner delimiter on values of nextLine

I have a file in .dat format. Here is a sample


||= (N ) =|| 1|| 0.938 || --- || 0.5 || (****)|| 0.5 || 0 || 0 || 0 || 0.700 || (p)=2212, (n)=2112 ||

||= (\Delta ) =|| 2|| 1.232 || 0.118 || 1.5 || (****)|| 1.5 || 0 || 0 || 3 || 1.076 || (\Delta^{++})=2224, (\Delta^+)=2214, (\Delta^0)=2114, (\Delta^-)=1114 ||

||= (P_{11}(1440) ) =|| 3|| 1.462 || 0.391 || 0.5 || (****)|| 0.5 || 0 || 0 || 3 || 1.076 || 202212, 202112 ||

||= (S_{11}(1535) ) =|| 4|| 1.534 || 0.151 || 0.5 || ( ***)|| 0.5 || 0 || 0 || 3 || 1.076 || 102212, 102112 ||


I am trying to use Scanner to read this file and delimit the line by the "||" and then send into and ArrayList for future processing. Here is a sample of my code where I use the delimiter

String file = "data.dat";
Scanner s = null;
try {
s = new Scanner(new File(file)).useDelimiter("\\|\\|"); //here is the use of my delimiter

ArrayList<String> list = new ArrayList<String>();
while (s.hasNextLine()) { //notice I am using hasNextLine because each line must be unique to create the HashMap
list.add(s.nextLine());
}
s.close();
for (String string : list) { //Lets print out the values of the list
System.out.println(string);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


But my output still has values of the delimiter, i.e. here is the output:


||= (N ) =|| 1|| 0.938 || --- || 0.5 || (****)|| 0.5 || 0 || 0 || 0 || 0.700 || (p)=2212, (n)=2112 ||

||= (\Delta ) =|| 2|| 1.232 || 0.118 || 1.5 || (****)|| 1.5 || 0 || 0 || 3 || 1.076 || (\Delta^{++})=2224, (\Delta^+)=2214, (\Delta^0)=2114, (\Delta^-)=1114 ||

||= (P_{11}(1440) ) =|| 3|| 1.462 || 0.391 || 0.5 || (****)|| 0.5 || 0 || 0 || 3 || 1.076 || 202212, 202112 ||

||= (S_{11}(1535) ) =|| 4|| 1.534 || 0.151 || 0.5 || ( ***)|| 0.5 || 0 || 0 || 3 || 1.076 || 102212, 102112 ||


I have searched and found no answer that helped. I am also seeing a warning about a "Resource leak: '' is never closed" with the line

s = new Scanner(new File(file)).useDelimiter("\\|\\|");


which disappears if the line is broken into

s = new Scanner(new File(file));
s.useDelimiter("\\|\\|");


Any help is appreciated.

Answer

You don't need to use backslash escape. instead of \\|\\| as your delimiter, just split the string after it's read from the file.

String file = "data.dat";
Scanner s = null;
try {
    s = new Scanner(new File(file)); //no more delimiter. It's not needed
    ArrayList<String> list = new ArrayList<String>();
    while (s.hasNextLine()) {

        String[] strings = s.nextLine().split("[||]");
        for (int i = 0; i < strings.length; i++) {
            list.add(strings[i]);   
        }
    }
    s.close();
    for (String string : list) {  //Lets print out the values of the list
        System.out.println(string);
    }
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} 

This should fix it.

Comments