Using multiple scanners to read file line by line resulting in noSuchElementException - java

I am not sure while am receiving a no such element exception. It seems to be an issue with the scanner not reading my file correctly, but I am not sure where I am going wrong.
I am reading a file, then using the scanner to go line by line. But I get unusual behavior, such as missing lines or filenotfound exceptions when I try to do it.
public static void readMylifeLikeABook(String fileName,int maxItems) {
// Read file line by line with different elements on each line
int bookCount=0;
int movieCount = 0;
Book [] bookItem =new Book[maxItems];
Movie [] movieItem =new Movie[maxItems];
try {
File file = new File(fileName);
Scanner scanner = new Scanner(file);
Scanner line;
while (scanner.hasNext() && ((bookCount+movieCount)<maxItems)) {
line = new Scanner(scanner.nextLine()); // scan next line
if (line.next().contains("Movie")){
movieItem[movieCount]= new Movie();
movieItem[movieCount].setMediaType("Movie");
movieItem[movieCount].setTitle(scanner.nextLine());
movieItem[movieCount].setRef(scanner.nextLine());
movieItem[movieCount].setPrice(Double.valueOf(scanner.nextLine()));
movieItem[movieCount].setDirector(scanner.nextLine());
movieItem[movieCount].setActor(scanner.nextLine());
System.out.println(movieItem[movieCount].getTitle());
movieCount++;
line.close(); // close line
}
else if (scanner.next().contains("Book")){
bookItem[bookCount]= new Book();
bookItem[bookCount].setMediaType("Book");
bookItem[bookCount].setTitle(scanner.nextLine());
bookItem[bookCount].setRef(scanner.nextLine());
bookItem[bookCount].setPrice(Double.valueOf(scanner.nextLine()));
bookItem[bookCount].setAuthor(scanner.nextLine());
System.out.println(bookItem[bookCount].getTitle());
bookCount++;
line.close(); // close line
}
}
scanner.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
//System.out.println(count);
for (int i=0;i<(bookCount+movieCount);i++) {
System.out.println(bookItem[i] +"\n\n "+ movieItem[i]);
}
}

Hmm, is it possible if you can provide the file being read? This can very well be a problem with that File as if the Scanner object does not a following line to be read, it will throw an exception. Make sure the file has the needed amount of lines.

You call line.next() immediately after constructing line. Do you need to do this? If so, perhaps you need to call hasNext() first to ensure there's a token there.
(I'm pretty sure line.next() throws a NoSuchElementException if there's no token.)
Also, check your code against the file format. Are there blank lines in your file?

Related

CsvMalformedLineException: Unterminated quoted field at end of CSV line

I am writing code to process a list of tar.gz files, inside which there are multiple, csv files. I have encountered the error below
com.opencsv.exceptions.CsvMalformedLineException: Unterminated quoted field at end of CSV line. Beginning of lost text: [,,,,,,
]
at com.opencsv.CSVReader.primeNextRecord(CSVReader.java:245)
at com.opencsv.CSVReader.flexibleRead(CSVReader.java:598)
at com.opencsv.CSVReader.readNext(CSVReader.java:204)
at uk.ac.shef.inf.analysis.Test.readAllLines(Test.java:64)
at uk.ac.shef.inf.analysis.Test.main(Test.java:42)
And the code causing this problem is below, on line B.
public class Test {
public static void main(String[] args) {
try {
Path source = Paths.get("/home/xxxx/Work/data/amazon/labelled/small/Books_5.json.1.tar.gz");
InputStream fi = Files.newInputStream(source);
BufferedInputStream bi = new BufferedInputStream(fi);
GzipCompressorInputStream gzi = new GzipCompressorInputStream(bi);
TarArchiveInputStream ti = new TarArchiveInputStream(gzi);
CSVParser parser = new CSVParserBuilder().withStrictQuotes(true)
.withQuoteChar('"').withSeparator(',').
.withEscapeChar('|'). // Line A
build();
BufferedReader br = null;
ArchiveEntry entry;
entry = ti.getNextEntry();
while (entry != null) {
br = new BufferedReader(new InputStreamReader(ti)); // Read directly from tarInput
System.out.format("\n%s\t\t > %s", new Date(), entry.getName());
try{
CSVReader reader = new CSVReaderBuilder(br).withCSVParser(parser)
.build();
List<String[]> r = readAllLines(reader);
} catch (Exception ioe){
ioe.printStackTrace();
}
System.out.println(entry.getName());
entry=ti.getNextEntry(); // Line B
}
}catch (Exception e){
e.printStackTrace();
}
}
private static List<String[]> readAllLines(CSVReader reader) {
List<String[]> out = new ArrayList<>();
int line=0;
try{
String[] lineInArray = reader.readNext();
while(lineInArray!=null) {
//System.out.println(Arrays.asList(lineInArray));
out.add(lineInArray);
line++;
lineInArray=reader.readNext();
}
}catch (Exception e){
System.out.println(line);
e.printStackTrace();
}
System.out.println(out.size());
return out;
}
}
I also attach a screenshot of the actual line within the csv file that caused this problem here, look at line 5213. I also include a test tar.gz file here: https://drive.google.com/file/d/1qHfWiJItnE19-BFdbQ3s3Gek__VkoUqk/view?usp=sharing
While debugging, I have some questions.
I think the issue is the \ character in the data file (line 5213 above), which is the escape character in Java. I verified this idea by adding line A to my code above, and it works. However, obviously I don't want to hardcode this as there can be other characters in the data causing same issue. So my question 1 is: is there anyway to tell Java to ignore escape characters? Something like the opposite of withEscapeChar('|')? UPDATE: the answer is to use '\0', thanks to the first comment below.
When debugging, I notice that my program stops working on the next .csv file within the tar.gz file as soon as it hit the above exception. To explain what I mean, inside the tar.gz file included in the above link, there are two csvs: _10.csv and _110.csv. The problematic line is in _10.csv. When my program hit that line, an exception is thrown and the program moves on to the next file _110.csv (entry=ti.getNextEntry();). This file is actually fine, but the method readAllLines that is supposed to read this next csv file will throw the same exception immediately on the first line. I don't think my code is correct, especially the while loop: I suspect the input stream was still stuck at the previous position that caused the exception. But I don't know how to fix this. Help please?
using RFC4180Parser worked for me.

Writing multiple lines using PrintWriter in Java

I am sorry if this question is already answered on the internet but by looking at the 'similar questions' I couldn't find an answer for my problem.
So basically I'm trying to understand why using PrintWriter to write lines of text into a file apparently skips the first line every 2 lines I give.
This is the method I use in its own class:
public class IO_Utilities {
public static void Write(String outputName){
PrintWriter outputStream = null;
try {
outputStream = new PrintWriter(outputName);
} catch (FileNotFoundException e) {
System.out.println(e.toString());
System.exit(0);
}
System.out.println("Write your text:");
Scanner user = new Scanner(System.in);
while(!user.nextLine().equals("stop")) {
String line = user.nextLine();
outputStream.println(line);
}
outputStream.close();
user.close();
System.out.println("File has been written.");
}
When I call it from main with:
IO_Utilities.Write("output.txt");
and write:
first line
second line
third line
stop
stop
The output text is actually:
second line
stop
I am sure the problem is the while loop, because if I input the lines manually one by one it works correctly, but I don't understand exactly why it behaves like it does right now.
while(!user.nextLine().equals("stop")) {
String line = user.nextLine();
outputStream.println(line);
}
You repeat your user.nextLine(); within your loop, so line is not the same as the one you compare to "stop".
Rewrite that part as:
String line = user.nextLine();
while( !line.equals("stop")) {
outputStream.println(line);
line = user.nextLine();
}

Java Scanner not finding next line?

For some reason when I try scan a .txt file, it is failing to find any lines and thus causing the error:
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
Code:
File file = new File("C:\\Users\\kayc0\\Desktop\\CkayBotBets\\mods.txt");
Scanner scanner = new Scanner(new FileInputStream (file), "UTF-8");
while(scanner.hasNextLine()){
modsList.add(scanner.nextLine());
System.out.println(scanner.nextLine());
}
I do not close the scanner.
modsList is a List that I try add each line to so I can check if a mod exists in chat (user) matches one in the list, however the error is on System.out...
I checked the .txt file exists with the following:
File f = new File("C:\\Users\\kayc0\\Desktop\\CkayBotBets\\mods.txt");
if(f.exists() && !f.isDirectory()) {
System.out.println("file exists");
}
Anyone any idea why the lines are not being read?
.txt contents:
abkayckay
kayc01
Thanks, any help appreciated.
You are currently reading two lines instead of one, save the line you read to add to your list and display with the same line.
while(scanner.hasNextLine()){
String line = scanner.nextLine();
modsList.add(line);
System.out.println(line);
}
You call nextLine tice. Change to:
while(scanner.hasNextLine()){
String value = scanner.nextLine()
modsList.add(value);
System.out.println(value);
}

How to use try/catch to avoid throwing FileNotFoundException in java

I am trying to output a file scanner object from my method. This is a school assignment and I am specifically instructed to NOT throw any exceptions, but use try/catch instead. The assignment requires that the command line prompt the user for a file to scan. If the file does not exist, we are supposed to tell the user, then prompt them for a file again. If the file does exist, then the method returns a scanner object that scans the file.
My code works, but it is not clean. It involves 2 methods. This is my code so far:
public static Scanner getInputScanner (Scanner console) {
File inputFile = null;
Scanner input = null;
try {
inputFile = getFile(inputFile, console);
input = new Scanner (inputFile);
return input;
} catch (FileNotFoundException e) {
try {
return input = new Scanner (getFile (inputFile, console));
} catch (FileNotFoundException f) {
System.out.println("An error has occured.");
return input;
}
}
}
public static File getFile (File inputFile, Scanner console) {
System.out.println("Enter input file: ");
inputFile = new File (console.nextLine());
while (!inputFile.exists()) {
System.out.println("File does not exist.");
System.out.print("Enter input file: ");
inputFile = new File (console.nextLine());
}
return inputFile;
}
The problem with the code is that the output looks like this:
Enter input file:
File does not exist.
Enter input file:
It then is waiting for the user's input. I don't want the output to have the 2 lines of code before the last line though.
Can anybody explain why my code is outputting these 2 lines?
Also, is there a simpler solution to getting an input file without throwing the FileNotFoundException?
Thanks!
If I understand correctly,
your program outputs these lines when you run it,
no matter what,
without you getting a chance to actually enter a filename.
Enter input file:
File does not exist.
And then the programs asks you again:
Enter input file:
And you don't want the first two lines above, right?
This can happen for example if the Scanner console you received has an unread newline in it.
You haven't posted that part of the code,
so it's hard to tell, but this is a common gotcha with Scanner.
Before calling getInputScanner,
make sure the Scanner console is ready to use,
with no unread garbage still buffered in it.
As for the second part of your question,
yes this can be written simpler and better, for example:
public static Scanner getInputScanner(Scanner console) {
try {
File inputFile = getExistingFile(console);
return new Scanner(inputFile);
} catch (FileNotFoundException e) {
throw new AssertionError("The file is expected to exist (was supposed to be verified earlier)");
}
}
public static File getExistingFile(Scanner console) {
while (true) {
System.out.println("Enter input file: ");
File inputFile = new File(console.nextLine());
if (inputFile.exists()) {
return inputFile;
}
System.out.println("File does not exist.");
}
}
It execute below line as soon the getFile() being called.
System.out.print("Enter input file: ");
Since no file exist, the below lines keeps on executing :
while (!inputFile.exists()) {
System.out.println ("File does not exist.");
System.out.print("Enter input file: ");
You can use throws() instead of try/catch, then caller will take care of exception.
Had to consume whatever junk was being carried over from the scanner by inserting a Scanner.nextLine() before getting user input. Final code looks like this:
public static Scanner getInputScanner(Scanner console) {
try {
File inputFile = getExistingFile(console);
return new Scanner(inputFile);
} catch (FileNotFoundException e) {
throw new AssertionError("The file is expected to exist (was supposed to be verified earlier)");
}
}
public static File getExistingFile(Scanner console) {
while (true) {
console.nextLine();
System.out.println("Enter input file: ");
File inputFile = new File(console.nextLine());
if (inputFile.exists()) {
return inputFile;
}
System.out.println("File does not exist.");
}
}

How to read specific parts of a .txt file in JAVA

I have been trying to figure out how to read from a .txt file. I know how to read a whole file, but I am having difficulties reading between two specific points in a file.
I am also trying to use the scanner class and this is what I have so far:
public void readFiles(String fileString)throws FileNotFoundException{
file = new File(fileString);
Scanner scanner = null;
line="";
//access file
try {
scanner = new Scanner(file);
}
catch (FileNotFoundException e) {
System.out.println("File not found.");
}
// if more lines in file, go to next line
while (scanner.hasNext())
{
line = scanner.next();
if (scanner.equals("BGSTART")) //tag in the txt to locate position
{
line = scanner.nextLine();
System.out.println(line);
lb1.append(line); //attaches to a JTextArea.
window2.add(lb1);//adds to JPanel
}
}
.txt file looks something like this:
BGSTART
//content
BGEND
Nothing is posted onto the panel when I run the program.
I am trying to read it between those two points.I don't have a lot of experience in reading from txt file.
Any suggestions?
Thank You.
Assuming that BGSTART and BGEND are on seperate lines, as per #SubOptimal's question, you would need to do this:
boolean tokenFound = false;
while (scanner.hasNextLine())
{
line = scanner.nextLine();
//line, not scanner.
if (line.equals("BGSTART")) //tag in the txt to locate position
{
tokenFound = true;
}
else if (line.equals("BGEND"))
{
tokenFound = false;
}
if(tokenFound)
{
System.out.println(line);
lb1.append(line); //attaches to a JTextArea.
window2.add(lb1);//adds to JPanel
}
}
Some improvements:
try {
Scanner scanner = new Scanner(new FileInputStream(file));
//Moved the rest of the code within the try block.
//As it was before, if there where any problems loading the file, you would have gotten an error message (File not found)
//as per your catch block but you would then have gotten an unhandled null pointer exception when you would have tried to
//execute this bit: scanner.hasNextLine()
boolean tokenFound = false;
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
//line, not scanner.
if (line.equals("BGSTART")) //tag in the txt to locate position
{
tokenFound = true;
} else if (line.equals("BGEND")) {
tokenFound = false;
}
if ((tokenFound) && (!line.equals("BGSTART"))) {
System.out.println(line);
//I am not sure what is happening here.
//lb1.append(line); //attaches to a JTextArea.
//window2.add(lb1);//adds to JPanel
}
}
} catch (Exception e) {
System.out.println("File not found.");
}
File content:
do not show line one
do not show line two
BGSTART
this is a line
this is another line
this is a third line
BGEND
do not show line three
do not show line four
Why don't you use substring? once you have located your BGSTART and BGEND you can capture the string between it somewhere in the lines of the below code:
StringBuilder sb= new StringBuilder();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
sb.append(line);
}
String capturedString = sampleString.substring(sb.toString().indexOf("BGSTART"),
sb.toString().indexOf("BGEND"));
hope this helps
Almost good, but you are doing:
if (scanner.equals("BGSTART")) //tag in the txt to locate position
You should look for file content in line variable not scanner.
So try this:
if (line.equals("BGSTART")) //tag in the txt to locate position

Categories

Resources