nextLine值上的Java扫描程序分隔符
我有一个.dat格式的文件。这是一个样本 ||=(N)=|1|0.938|--|0.5|124;(***)|0.5 | 0 | 0 | 0.700 |(p)=2212,(N)=2112|| ||=(\Delta)=| | 2 | | 1.232 | | | 0.118 | | | 1.5 | | | 1.5 | | | 0 | | | | 1.076 | | |(\Delta^{++})=2224、(\Delta^+)=2214、(\Delta^0)=2114、(^-)=1114|| ||=(p{11}(1440))=| 3 | 1.462 | 0.391 | 0.5 | |(***)0.5 | | 0 | | 3 | | 1.076 | 202212、202112|| ||=(S|11}(1535))=|4 | 1.534 | 0.151 | 0.5 | |(***)0.5 | 0 | 3 | 1.076 | 102212、102112|| 我正在尝试使用扫描仪读取此文件,并用“| |”分隔线,然后发送到和ArrayList以供将来处理。下面是我使用分隔符的代码示例nextLine值上的Java扫描程序分隔符,java,Java,我有一个.dat格式的文件。这是一个样本 ||=(N)=|1|0.938|--|0.5|124;(***)|0.5 | 0 | 0 | 0.700 |(p)=2212,(N)=2112|| ||=(\Delta)=| | 2 | | 1.232 | | | 0.118 | | | 1.5 | | | 1.5 | | | 0 | | | | 1.076 | | |(\Delta^{++})=2224、(\Delta^+)=2214、(\Delta^0)=2114、(^-)=1114|| ||=(p
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();
}
如果线路被打断,它就会消失
s = new Scanner(new File(file));
s.useDelimiter("\\|\\|");
非常感谢您的帮助。您不需要使用反斜杠转义。不要将
\\\\\\\\\\\\
作为分隔符,只需在从文件中读取字符串后将其拆分即可
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();
}
String file=“data.dat”;
扫描器s=null;
试一试{
s=新扫描程序(新文件(File));//不再使用分隔符。不需要
ArrayList=新建ArrayList();
而(s.hasNextLine()){
String[]strings=s.nextLine().split([| |]]);
for(int i=0;i
这应该可以解决问题。您只需读取不带分隔符的行,然后使用您想要的任何方法替换
|
代码
publicstaticvoidmain(字符串[]args)抛出java.lang.Exception{
扫描仪s=新的扫描仪(System.in);
ArrayList=新建ArrayList();
而(s.hasNextLine()){
字符串换行符=s.nextLine().replace(“| |”,”);
list.add(换行符);
//还是像这样
//list.add(s.nextLine().replace(“| |“,”);
}
s、 close();
对于(字符串:列表){
System.out.println(字符串);
}
}
检查输出。尝试使用
next()
而不是nextLine()
。这是因为一行不一定以|
@progy\u结尾。rock nextLine是必不可少的,因为每一行都是一个值数组,我以后需要排序。使用next()将丢失idenity的位置。为什么不使用不带分隔符的扫描仪读取每一行。然后在每一行使用String.split和分隔符?@mkunkel继续进行时,您试图同时满足两个相互排斥的条件。正如我所说,“一行不一定以| |
结尾”。行是以\n
结尾的行。在行尾前可能有|
s;我们怎么知道没有呢?@progy_rock,你可能对其中一个答案中的OP评论感兴趣我需要使用nextLine(),而不是next()。稍后,我将使用一些逻辑来比较每行的数组值。因此,我需要nextLine()。
因此,这里缺少信息。如果OP以后需要处理一行,只需删除分隔符即可。使用拆分创建他的数组。阵列将是一个完整的行。我已经修改了你的建议,这没有工作。我得到了和以前一样的输出,就一秒钟。再次测试,看看你在那里做了什么。我需要使用nextLine(),而不是next()。稍后,我将使用一些逻辑来比较每行的数组值。所以我需要nextLine()。@mkunkel我很乐意,请注意,如果使用replace而不是split,可以避免内部for循环,这反过来可能会提高大型文件的性能。
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();
}
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);
}
}