Reputation: 27
First off, This is what the data file may look like
5165168416516988798484984984984984
9898798798798
1556516846516518498
51688484979789798798798491
I need to read both lines before that line break (which I can do fine), and continue reading two by two until the file ends. There is a guaranteed even number of pairings so there will not be a solitary line at the end. My problem is trying to read the file beyond the first two lines.
code
try {
Scanner input = new Scanner (file);
while (input.hasNextLine()) {
String number1 = input.nextLine();
String number2 = input.nextLine();
System.out.println("String: " + number1);
System.out.println("String: " + number2);
System.out.println(number1 + " " + number2);
number1= input.nextLine();
number2= input.nextLine();
}
}
Is what I have done so far. It works ONLY if there are no line breaks at all, though it still shoots out an exception ("No Line Found") at the end.
This exception occurs as soon as the first break is found in the file. How do I make it so that the entire file reads in the way I desire? (Grab the two lines, do something to them. Grab the next two lines, do something... all the way until the end)
Upvotes: 2
Views: 1518
Reputation: 533690
You could ignore blank lines and it wouldn't matter if they were there or not.
public static String readNonBlankLine(Scanner in) {
while(in.hasNextLine()) {
String s = in.nextLine();
if (!s.trim().isEmpty())
return s;
}
return null;
}
try {
Scanner input = new Scanner (file);
while (input.hasNextLine()) {
String number1 = readNonBlankLine(input);
String number2 = readNonBlankLine(input);
System.out.println("String: " + number1);
System.out.println("String: " + number2);
System.out.println(number1 + " " + number2);
}
}
Upvotes: 1
Reputation: 4753
I have made fully working code. When this code reads past the last line, an exception NoSuchElementException
will be thrown. BUT, we catch this exception and let the user know that the end of file has been reached. Alternately, you could do nothing instead. But, that is not good.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Filez {
public static void main(String[] args) {
File file = null;
Scanner input = null;
try {
file = new File("C:/Temp/two.txt");
input = new Scanner(file);
String line = "";
int count = 0;
while (true) {
for(count = 1; count < 3; count++){
line = line + input.nextLine() + ", ";
}
System.out.println(line);
input.nextLine();
line = "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(NoSuchElementException e){
System.out.println("\n\nReached end of file...\n");
}
}
}
Upvotes: 0
Reputation: 22280
Ok. Let's try.
try {
Scanner input = new Scanner (file);
// input.hasNextLine() checks if there are more lines that can be read from
// the file. If there is a line, hasNextLine() will return true, and the code
// inside the while block will be executed.
// Then execution will come back here to perform the check again. This
// goes on until there are no more lines to consume.
while (input.hasNextLine()) {
// We are going to read two numbers from file.
// Ideally, we should check if there a line available before reading
// each of these lines. But since you confirm that the file will
// always have multiple number of triplets containing two lines
// with numbers, followed by a blank line, we are not going to
// call input.hasNextLine() here.
String number1 = input.nextLine();
String number2 = input.nextLine();
// Now, two numbers are read, read the next (empty) line to a variable
// but we will not use it anywhere.
String emptyLine = input.nextLine();
// ...or we could read it, but just discard it without assigning it to a variable
// input.nextLine();
// Print what we read to the output, like a boss.
System.out.println("String: " + number1);
System.out.println("String: " + number2);
System.out.println(number1 + " " + number2);
// This is not needed here (see below to understand why):
//number1= input.nextLine();
//number2= input.nextLine();
// after the following brace (}), execution will go back to the start
// of the while loop. and if there are more lines to be read, code
// inside while loop will be executed again.
}
}
Hope this clears it up.
Warning: For this code to work without an error, your input file must contain an empty line after the very last number pair in it. Otherwise it will throw a NoSuchElementException
.
Upvotes: 1
Reputation: 4897
This uses another way of reading a text file per line:
try {
BufferedReader newBufferedReader = Files.newBufferedReader(Paths.get("PathToTextFile.txt"), Charset.defaultCharset());
while (newBufferedReader.ready()) {
System.out.println(newBufferedReader.readLine());
}
newBufferedReader.close();
} catch (IOException ex) {
Logger.getLogger(NewJFrame1.class.getName()).log(Level.SEVERE, null, ex);
}
Upvotes: 0
Reputation: 698
Take a look at the modified code,particularly, lines with comments.
try {
Scanner input = new Scanner (file);
while (input.hasNextLine()) {
String number1 = input.nextLine();
String number2 = input.nextLine();
input.nextLine(); //this takes care of empty line
System.out.println("String: " + number1);
System.out.println("String: " + number2);
System.out.println(number1 + " " + number2);
}
}
Upvotes: 0