[英]Java Scanner delimiter on values of nextLine
I have a file in .dat format. 我有一个.dat格式的文件。 Here is a sample 这是一个例子
||= (N ) =|| || =(N)= || 1|| 1 || 0.938 || 0.938 || --- || --- || 0.5 || 0.5 || (****)|| (****)|| 0.5 || 0.5 || 0 || 0 || 0 || 0 || 0 || 0 || 0.700 || 0.700 || (p)=2212, (n)=2112 || (p)= 2212,(n)= 2112 ||
||= (\\Delta ) =|| || =(\\ Delta)= || 2|| 2 || 1.232 || 1.232 || 0.118 || 0.118 || 1.5 || 1.5 || (****)|| (****)|| 1.5 || 1.5 || 0 || 0 || 0 || 0 || 3 || 3 || 1.076 || 1.076 || (\\Delta^{++})=2224, (\\Delta^+)=2214, (\\Delta^0)=2114, (\\Delta^-)=1114 || (\\ Delta ^ {++})= 2224,(\\ Delta ^ +)= 2214,(\\ Delta ^ 0)= 2114,(\\ Delta ^ - )= 1114 ||
||= (P_{11}(1440) ) =|| || =(P_ {11}(1440))= || 3|| 3 || 1.462 || 1.462 || 0.391 || 0.391 || 0.5 || 0.5 || (****)|| (****)|| 0.5 || 0.5 || 0 || 0 || 0 || 0 || 3 || 3 || 1.076 || 1.076 || 202212, 202112 || 202212,202112 ||
||= (S_{11}(1535) ) =|| || =(S_ {11}(1535))= || 4|| 4 || 1.534 || 1.534 || 0.151 || 0.151 || 0.5 || 0.5 || ( ***)|| (***)|| 0.5 || 0.5 || 0 || 0 || 0 || 0 || 3 || 3 || 1.076 || 1.076 || 102212, 102112 || 102212,102112 ||
I am trying to use Scanner to read this file and delimit the line by the "||" 我正在尝试使用Scanner读取此文件并用“||”分隔行 and then send into and ArrayList for future processing. 然后发送到和ArrayList以供将来处理。 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, ie here is the output: 但我的输出仍然有分隔符的值,即这里是输出:
||= (N ) =|| || =(N)= || 1|| 1 || 0.938 || 0.938 || --- || --- || 0.5 || 0.5 || (****)|| (****)|| 0.5 || 0.5 || 0 || 0 || 0 || 0 || 0 || 0 || 0.700 || 0.700 || (p)=2212, (n)=2112 || (p)= 2212,(n)= 2112 ||
||= (\\Delta ) =|| || =(\\ Delta)= || 2|| 2 || 1.232 || 1.232 || 0.118 || 0.118 || 1.5 || 1.5 || (****)|| (****)|| 1.5 || 1.5 || 0 || 0 || 0 || 0 || 3 || 3 || 1.076 || 1.076 || (\\Delta^{++})=2224, (\\Delta^+)=2214, (\\Delta^0)=2114, (\\Delta^-)=1114 || (\\ Delta ^ {++})= 2224,(\\ Delta ^ +)= 2214,(\\ Delta ^ 0)= 2114,(\\ Delta ^ - )= 1114 ||
||= (P_{11}(1440) ) =|| || =(P_ {11}(1440))= || 3|| 3 || 1.462 || 1.462 || 0.391 || 0.391 || 0.5 || 0.5 || (****)|| (****)|| 0.5 || 0.5 || 0 || 0 || 0 || 0 || 3 || 3 || 1.076 || 1.076 || 202212, 202112 || 202212,202112 ||
||= (S_{11}(1535) ) =|| || =(S_ {11}(1535))= || 4|| 4 || 1.534 || 1.534 || 0.151 || 0.151 || 0.5 || 0.5 || ( ***)|| (***)|| 0.5 || 0.5 || 0 || 0 || 0 || 0 || 3 || 3 || 1.076 || 1.076 || 102212, 102112 || 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. 任何帮助表示赞赏。
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. 这应该解决它。
You can just read the line without delimiter, and then replace ||
您可以只读取没有分隔符的行,然后替换||
with whatever you want using replace
method. 用你想要的任何replace
方法。
Code 码
public static void main (String[] args) throws java.lang.Exception{
Scanner s = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
while (s.hasNextLine()) {
String newLine = s.nextLine().replace("||","");
list.add(newLine);
// or just like this
//list.add(s.nextLine().replace("||",""));
}
s.close();
for (String string : list) {
System.out.println(string);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.