Reputation: 468
I'm trying to read tokens and its values in a multiline text file, that has a format I specify bellow, so far I've read the file with Scanner and with a regular expression I'm able to differentiate the tokens (because they will always have the word "process" followed by a number). This is an example of the desired result.
objects[
[process0, [1,2,3,4,5]],
[process1, [6,7,8,9,10]],
[process2, [1,2]]
]
I want to place the following line inside the while loop to clean the array every time after adding a new object to the list of objects (since my approach of having a while loop in the else block didn't work, I left it outside and I didn't include that loop)
List<String> process_numbers = new ArrayList<String>();
My idea is to read a token then take the numbers before the next token and store them in an object and then adding the object to the list of objects, so basically I need help with that.
Note: I'm clearly not good at Java, feel free to suggest ways to improve my code.
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
EDIT:
I have modified the while loop this way:
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());
}
I'm kind of getting the result I want but with some problems, first the name of the processes are all the same ("process0") and the program is not printing the very last number of the text file.
Result so far
process0
[1, 2, 3, 4, 5]
process0
[6, 7, 8, 9, 10]
process0
[1]
Upvotes: 2
Views: 3388
Reputation: 449
You almost got it, but you were always skiping the line with process name
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);
}
Output:
process0
[1, 2, 3, 4, 5]
process1
[6, 7, 8, 9, 10]
process2
[1, 2]
Upvotes: 1