Reputation: 143
I am working with BufferedReader in Java and was hoping for some guidance when it comes to reading integers.
To summarize, each line of the input file will represent one edge in an undirected graph. It will contain two integers, the endpoints of the edge, followed by a real number, the weight of the edge. The last line will contain a -1, to denote the end of input.
I have created a BufferedReader object and initialized an integer variable and
The format of the file is as follows:
0 1 5.0
1 2 5.0
2 3 5.0
...
5 10 6.0
5 11 4.0
17 11 4.0
-1
public static void processFile(String inputFilePath) throws IOException {
//Check to see if file input is valid
if (inputFilePath == null || inputFilePath.trim().length() == 0) {
throw new IllegalArgumentException("Error reading file.");
}
//Initialize required variables for processing the file
int num = 0;
int count = 0;
try {
//We are reading from the file, so we can use FileReader and InputStreamReader.
BufferedReader fileReader = new BufferedReader(new FileReader(inputFilePath));
//Read numbers from the line
while ((num = fileReader.read()) != -1) { //Stop reading file when -1 is reached
//First input is the start
//Second input is the end
//Third input is the weight
}
} catch (IOException e) {
throw new IOException("Error processing the file.");
}
}
This is what I have attempted thus far, but I wondering how I can take each line of code, and have the first number be the "start" variable, the second number be the "end" variable, and the third number be the "weight" variable? I saw some solutions online to create an array but because of the format of my file I am somewhat confused. I can help clarify any details about
Upvotes: 1
Views: 4930
Reputation: 201437
I would start by checking that I can read the file (you can use File.canRead()
to do that). Next, I would compile a regular expression with three grouping operations. Then I would use BufferedReader.readLine()
to read lines of text; the read()
call returns a single character. Then it only remains to parse matching lines. And I see no purpose in swallowing the original exception only to rethrow it (in fact, you lose all stack trace information your current way). Putting that all together,
public static void processFile(String inputFilePath) throws IOException {
File f = new File(inputFilePath);
if (!f.canRead()) {
throw new IllegalArgumentException("Error reading file.");
}
// Initialize required variables for processing the file
try (BufferedReader fileReader = new BufferedReader(new FileReader(inputFilePath))) {
Pattern p = Pattern.compile("^\\s*(\\d+)\\s+(\\d+)\\s+(\\d.+)$");
String line;
while ((line = fileReader.readLine()) != null) {
Matcher m = p.matcher(line);
if (m.matches()) {
int start = Integer.parseInt(m.group(1));
int end = Integer.parseInt(m.group(2));
double weight = Double.parseDouble(m.group(3));
System.out.printf("start=%d, end=%d, weight=%.2f%n", start, end, weight);
}
}
}
}
Upvotes: 2
Reputation: 86
Instead of using read
you can just use readLine
then use split with your separator being three spaces I think?
try (BufferedReader fileReader = new BufferedReader(new FileReader(inputFilePath))) {
String line;
while(!(line = fileReader.readLine()).equals("-1")) {
String[] edge = line.split(" ");
int start = Integer.parseInt(edge[0]);
int end = Integer.parseInt(edge[1]);
double weight = Double.parseDouble(edge[2]);
}
} catch (IOException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 1902
Switch up to readLine and use a Scanner:
public static void processFile(String inputFilePath) throws IOException {
// Check to see if file input is valid
if (inputFilePath == null || inputFilePath.trim()
.length() == 0) {
throw new IllegalArgumentException("Error reading file.");
}
// Initialize required variables for processing the file
String line;
int count = 0;
// We are reading from the file, so we can use FileReader and InputStreamReader.
try (BufferedReader fileReader = new BufferedReader(new FileReader(inputFilePath))) {
// Read numbers from the line
while ((line = fileReader.readLine()) != null) { // Stop reading file when -1 is reached
Scanner scanner = new Scanner(line);
// First input is the start
int start = scanner.nextInt();
if (start == -1) {
break;
}
// Second input is the end
int end = scanner.nextInt();
// Third input is the weight
double weight = scanner.nextDouble();
// do stuff
}
} catch (IOException e) {
throw new IOException("Error processing the file.");
}
}
Upvotes: 0