How can I implement a method to return the line-number of the line currently being scanned from a file. I have two scanners, one for the file (fileScanner) and another for the line (lineScanner)
this is what I have, but I don't know if I need the linenumber in the constructor!
public TextFileScanner(String fileName) throws FileNotFoundException
{
this.fileScanner = new Scanner(new File(fileName));
this.lineScanner = new Scanner(this.fileScanner.nextLine());
this.lineNumber = 1;
}
and I need this method:
public int getLineNumber()
{
}
You can use just one
Scanner
object to read a file and reports the line numbers.Here is a sample code:
Now, if you want do this using an Object-Oriented Programming approach then you can do something like this:
Prints the Current line number:
System.out.println("The line number is " + new Exception().getStackTrace()[0].getLineNumber());
Example:
public class LineNumberTest{
}