0

I am creating a CSV parser library in Java. I have the following code so far:

However I keep getting the error when I try to include user input to the ("Enter a delimiter") part:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at demo.CSV.main(CSV.java:19)

Also can you please help me figure out how I would create a test application that can use the library.

Thank you.

package demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CSV {

public static void main(String[] args) throws FileNotFoundException {

Scanner x = new Scanner(System.in);
System.out.println("Enter the File");
String s = x.next();
x.close();   
Scanner scanner = new Scanner(new File(s));
scanner.useDelimiter(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
System.out.println("Enter delimiter");
Scanner scanner1 = new Scanner(System.in);
String format = scanner1.nextLine();
while(scanner.hasNext()){
System.out.println(scanner.next()+format);        
}
scanner.close();
}
}
1
  • x.close(); also closes System.in. scanner1 won't be able to use it anymore. Use just one scanner for that inputstream. Commented Feb 4, 2018 at 16:11

1 Answer 1

1

The problem boils down to the fact that you're not using Scanner.nextLine() properly, compounded by the fact that your input file is empty.

When you use nextLine(), you need to enclose it within a loop that checks to see if an input source has a next line using hasNextLine() before trying to read the line with nextLine().

Your code assumes that there's a next line without first checking. Meanwhile, your input file is empty, so you get NoSuchElementException.

Instead of going in blind like this:

String format = scanner1.nextLine();

Replace that line with this:

String format = null;

while(scanner1.hasNext())
{
    format = scanner1.nextLine();
}

Then make sure you generate an input file that actually has one or more lines in it.

Sign up to request clarification or add additional context in comments.

4 Comments

I have corrected the code, but I really need help in creating a test application for the library as I do not know how to implement this.
@TrailRain If you've understood and corrected the code, then your original question has been answered. Click the checkmark to the left of the answer to let people know what helped you. Then revert your post to its original form (asking about NoSuchElementException) and post the new question about testing separately. Your edits make the question and answers fall out of synchronization. Stack Overflow uses a one question, one answer format. You can't keep changing things because you're asking people to hit a moving target, which is a lot to ask in exchange for free help.
Oh ok I am new to this sorry about that
@TrailRain No problem. Please familiarize yourself with the Stack Overflow help file which will explain how this site works, and especially how to ask good questions which receive good answers. In my experience, the questions that people ignore are poorly formed (leaving out important information), whereas well formed questions tend to get answers. SO is strict about one question, one answer because it makes finding answers easier for people asking about the same thing you are.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.