我正在尝试在多行文本文件中读取令牌及其值,该文件具有我指定的格式,到目前为止我已经使用Scanner读取文件并使用正则表达式我能够区分令牌(因为他们总是会有“过程”这个词后跟一个数字)。这是所需结果的一个例子。
objects[
[process0, [1,2,3,4,5]],
[process1, [6,7,8,9,10]],
[process2, [1,2]]
]
我想在while循环中放置以下行,每次将新对象添加到对象列表后清理数组(因为我在else块中使用while循环的方法不起作用,我离开了在外面,我没有包括那个循环)
List<String> process_numbers = new ArrayList<String>();
我的想法是读取一个令牌,然后在下一个令牌之前取数字并将它们存储在一个对象中,然后将该对象添加到对象列表中,所以基本上我需要帮助。
注意:我显然不擅长Java,可以随意提出改进代码的方法。
Main.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
File f = new File("processes.txt");
Scanner sc = new Scanner(f);
List<Objects> objects = new ArrayList<Objects>();
String process_name = " ";
List<String> process_numbers = new ArrayList<String>();
while(sc.hasNextLine()){
String line = sc.nextLine();
if (line.matches(".*process.*")){
process_name = line;
//System.out.println("PROCESS NAME: " + line);
}else{
process_numbers.add(line);
//System.out.println("NUMBER: " + line);
}
Objects o = new Objects(process_name, process_numbers);
objects.add(o);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Objects.java
import java.util.ArrayList;
import java.util.List;
public class Objects {
private String process_name;
List<String> process_numbers = new ArrayList<String>();
public Objects(String process_name, List<String> process_numbers){
this.process_name = process_name;
this.process_numbers = process_numbers;
}
public String getProcess_name() {
return process_name;
}
public void setProcess_name(String process_name) {
this.process_name = process_name;
}
public List<String> getProcess_numbers() {
return process_numbers;
}
public void setProcess_numbers(List<String> process_numbers) {
this.process_numbers = process_numbers;
}
}
processes.txt
process0
1
2
3
4
5
process1
6
7
8
9
10
process2
1
2
编辑:
我已经用这种方式修改了while循环:
while(sc.hasNextLine()) {
String line = sc.nextLine();
List<String> process_numbers = new ArrayList<String>();
if (line.matches(".*process.*")) {
process_name = line;
line = sc.nextLine();
}
while (!line.matches(".*process.*") && sc.hasNextLine()) {
process_numbers.add(line);
line = sc.nextLine();
}
Objects o = new Objects(process_name, process_numbers);
objects.add(o);
}
for(Objects obj : objects) {
System.out.println(obj.getProcess_name());
System.out.println(obj.getProcess_numbers());
}
我有点得到我想要的结果但有一些问题,首先进程的名称都是相同的(“process0”)并且程序没有打印文本文件的最后一个数字。
到目前为止的结果
process0
[1, 2, 3, 4, 5]
process0
[6, 7, 8, 9, 10]
process0
[1]
答案 0 :(得分:0)
你几乎得到了它,但你总是用进程名称
来划线String line = sc.nextLine();
while(sc.hasNextLine()) {
List<String> process_numbers = new ArrayList<String>();
if (line.matches(".*process.*")) {
process_name = line;
line = sc.nextLine();
}
while (!line.matches(".*process.*") && sc.hasNextLine()) {
process_numbers.add(line);
line = sc.nextLine();
}
if(!sc.hasNextLine()){
process_numbers.add(line);
}
Objects o = new Objects(process_name, process_numbers);
objects.add(o);
}
输出:
process0
[1, 2, 3, 4, 5]
process1
[6, 7, 8, 9, 10]
process2
[1, 2]