Is there a way to check if Java has started reading a new line in a text file when reading in a text file












-1














I'm creating a program which reads in large chunks of data and i will need to separate them and i wanted to know if there is a function in java which would notify me when Java starts reading in a new line, by the way I am using scanners to read in my text files, these files are also CSV files if that changes anything.



I've tried looking online for any way of solving this and also read some of the functions of what a scanner can do and couldn't find anything useful



public class ScannerReading {



public static void main(String args) throws FileNotFoundException {

File file = new File("C:\myfile.txt");
Scanner scanner = new Scanner(file);
scanner.useDelimiter(",");
String data = scanner.nextLine();
data = scanner.nextLine();

while(scanner.hasNext()){
if(data.contains(" ")) {
System.out.println("I have a line lol");
}
System.out.print(data+" ");
}
scanner.close();
}


}



I am expecting an output of Line 1: INFORMATION EXTRACTED FROM THE FIRST LINE










share|improve this question


















  • 5




    Why reinvent the wheel? Why don't you use a CSV reader to read CSV files? If you really want to use a scanner, then read the file line by line using a scanner, and parse each line using another scanner. But beware that CSV is more complex than you might think. values can contain commas and new lines, if theyr enclosed in double quotes. A good CSV parser handles that.
    – JB Nizet
    Dec 27 '18 at 22:02












  • Oh, I never new that there was a CSV reader thanks for the tip
    – F.Zajac
    Dec 27 '18 at 22:06






  • 4




    You're not the first person on earth trying to parse CSV in Java. Thousands of people have done that before you. It's true of basically everything, and Java has a huge ecosystem of opensource libraries
    – JB Nizet
    Dec 27 '18 at 22:10










  • doesn't each iteration of the while loop tell you that it's a new line?
    – Zeratul
    Dec 27 '18 at 22:46










  • I guess i could maybe use some sort of counter to count each time it checks, thank you very much
    – F.Zajac
    Dec 27 '18 at 22:53
















-1














I'm creating a program which reads in large chunks of data and i will need to separate them and i wanted to know if there is a function in java which would notify me when Java starts reading in a new line, by the way I am using scanners to read in my text files, these files are also CSV files if that changes anything.



I've tried looking online for any way of solving this and also read some of the functions of what a scanner can do and couldn't find anything useful



public class ScannerReading {



public static void main(String args) throws FileNotFoundException {

File file = new File("C:\myfile.txt");
Scanner scanner = new Scanner(file);
scanner.useDelimiter(",");
String data = scanner.nextLine();
data = scanner.nextLine();

while(scanner.hasNext()){
if(data.contains(" ")) {
System.out.println("I have a line lol");
}
System.out.print(data+" ");
}
scanner.close();
}


}



I am expecting an output of Line 1: INFORMATION EXTRACTED FROM THE FIRST LINE










share|improve this question


















  • 5




    Why reinvent the wheel? Why don't you use a CSV reader to read CSV files? If you really want to use a scanner, then read the file line by line using a scanner, and parse each line using another scanner. But beware that CSV is more complex than you might think. values can contain commas and new lines, if theyr enclosed in double quotes. A good CSV parser handles that.
    – JB Nizet
    Dec 27 '18 at 22:02












  • Oh, I never new that there was a CSV reader thanks for the tip
    – F.Zajac
    Dec 27 '18 at 22:06






  • 4




    You're not the first person on earth trying to parse CSV in Java. Thousands of people have done that before you. It's true of basically everything, and Java has a huge ecosystem of opensource libraries
    – JB Nizet
    Dec 27 '18 at 22:10










  • doesn't each iteration of the while loop tell you that it's a new line?
    – Zeratul
    Dec 27 '18 at 22:46










  • I guess i could maybe use some sort of counter to count each time it checks, thank you very much
    – F.Zajac
    Dec 27 '18 at 22:53














-1












-1








-1







I'm creating a program which reads in large chunks of data and i will need to separate them and i wanted to know if there is a function in java which would notify me when Java starts reading in a new line, by the way I am using scanners to read in my text files, these files are also CSV files if that changes anything.



I've tried looking online for any way of solving this and also read some of the functions of what a scanner can do and couldn't find anything useful



public class ScannerReading {



public static void main(String args) throws FileNotFoundException {

File file = new File("C:\myfile.txt");
Scanner scanner = new Scanner(file);
scanner.useDelimiter(",");
String data = scanner.nextLine();
data = scanner.nextLine();

while(scanner.hasNext()){
if(data.contains(" ")) {
System.out.println("I have a line lol");
}
System.out.print(data+" ");
}
scanner.close();
}


}



I am expecting an output of Line 1: INFORMATION EXTRACTED FROM THE FIRST LINE










share|improve this question













I'm creating a program which reads in large chunks of data and i will need to separate them and i wanted to know if there is a function in java which would notify me when Java starts reading in a new line, by the way I am using scanners to read in my text files, these files are also CSV files if that changes anything.



I've tried looking online for any way of solving this and also read some of the functions of what a scanner can do and couldn't find anything useful



public class ScannerReading {



public static void main(String args) throws FileNotFoundException {

File file = new File("C:\myfile.txt");
Scanner scanner = new Scanner(file);
scanner.useDelimiter(",");
String data = scanner.nextLine();
data = scanner.nextLine();

while(scanner.hasNext()){
if(data.contains(" ")) {
System.out.println("I have a line lol");
}
System.out.print(data+" ");
}
scanner.close();
}


}



I am expecting an output of Line 1: INFORMATION EXTRACTED FROM THE FIRST LINE







java file






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 27 '18 at 21:58









F.Zajac

32




32








  • 5




    Why reinvent the wheel? Why don't you use a CSV reader to read CSV files? If you really want to use a scanner, then read the file line by line using a scanner, and parse each line using another scanner. But beware that CSV is more complex than you might think. values can contain commas and new lines, if theyr enclosed in double quotes. A good CSV parser handles that.
    – JB Nizet
    Dec 27 '18 at 22:02












  • Oh, I never new that there was a CSV reader thanks for the tip
    – F.Zajac
    Dec 27 '18 at 22:06






  • 4




    You're not the first person on earth trying to parse CSV in Java. Thousands of people have done that before you. It's true of basically everything, and Java has a huge ecosystem of opensource libraries
    – JB Nizet
    Dec 27 '18 at 22:10










  • doesn't each iteration of the while loop tell you that it's a new line?
    – Zeratul
    Dec 27 '18 at 22:46










  • I guess i could maybe use some sort of counter to count each time it checks, thank you very much
    – F.Zajac
    Dec 27 '18 at 22:53














  • 5




    Why reinvent the wheel? Why don't you use a CSV reader to read CSV files? If you really want to use a scanner, then read the file line by line using a scanner, and parse each line using another scanner. But beware that CSV is more complex than you might think. values can contain commas and new lines, if theyr enclosed in double quotes. A good CSV parser handles that.
    – JB Nizet
    Dec 27 '18 at 22:02












  • Oh, I never new that there was a CSV reader thanks for the tip
    – F.Zajac
    Dec 27 '18 at 22:06






  • 4




    You're not the first person on earth trying to parse CSV in Java. Thousands of people have done that before you. It's true of basically everything, and Java has a huge ecosystem of opensource libraries
    – JB Nizet
    Dec 27 '18 at 22:10










  • doesn't each iteration of the while loop tell you that it's a new line?
    – Zeratul
    Dec 27 '18 at 22:46










  • I guess i could maybe use some sort of counter to count each time it checks, thank you very much
    – F.Zajac
    Dec 27 '18 at 22:53








5




5




Why reinvent the wheel? Why don't you use a CSV reader to read CSV files? If you really want to use a scanner, then read the file line by line using a scanner, and parse each line using another scanner. But beware that CSV is more complex than you might think. values can contain commas and new lines, if theyr enclosed in double quotes. A good CSV parser handles that.
– JB Nizet
Dec 27 '18 at 22:02






Why reinvent the wheel? Why don't you use a CSV reader to read CSV files? If you really want to use a scanner, then read the file line by line using a scanner, and parse each line using another scanner. But beware that CSV is more complex than you might think. values can contain commas and new lines, if theyr enclosed in double quotes. A good CSV parser handles that.
– JB Nizet
Dec 27 '18 at 22:02














Oh, I never new that there was a CSV reader thanks for the tip
– F.Zajac
Dec 27 '18 at 22:06




Oh, I never new that there was a CSV reader thanks for the tip
– F.Zajac
Dec 27 '18 at 22:06




4




4




You're not the first person on earth trying to parse CSV in Java. Thousands of people have done that before you. It's true of basically everything, and Java has a huge ecosystem of opensource libraries
– JB Nizet
Dec 27 '18 at 22:10




You're not the first person on earth trying to parse CSV in Java. Thousands of people have done that before you. It's true of basically everything, and Java has a huge ecosystem of opensource libraries
– JB Nizet
Dec 27 '18 at 22:10












doesn't each iteration of the while loop tell you that it's a new line?
– Zeratul
Dec 27 '18 at 22:46




doesn't each iteration of the while loop tell you that it's a new line?
– Zeratul
Dec 27 '18 at 22:46












I guess i could maybe use some sort of counter to count each time it checks, thank you very much
– F.Zajac
Dec 27 '18 at 22:53




I guess i could maybe use some sort of counter to count each time it checks, thank you very much
– F.Zajac
Dec 27 '18 at 22:53












1 Answer
1






active

oldest

votes


















2














The direct answer to your question is that there is no way to find out if you have just started a new line when you use Scanner::next / Scanner::hasNext. And more generally, there is no way to find out what the last delimiter was. The delimiters are discarded.



As JB Nizet says there are lots of existing open source CSV reader libraries, so there is no need to implement this functionality using Scanner. Indeed, implementing CSV reading properly is not trivial, especially if you need to implement headers, quoting, escaping and/or continuation lines. Using an existing library is advisable.



But if (against advice!) you decide to implement the reader directly, then a more robust approach is to use a nested loop:




  • The outer loop reads complete lines using nextLine

  • The inner loop creates a Scanner for each line to split it into fields.


Except that that doesn't deal with quoting, escaping, continuation lines, etc. The real problem is that the CSV grammar doesn't have a simple context independent delimiter.




I guess I could maybe use some sort of counter to count [fields]




Yea ... but if some of the lines in your CSV are missing fields (e.g. due to a human error) then counting the fields won't detect this.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951275%2fis-there-a-way-to-check-if-java-has-started-reading-a-new-line-in-a-text-file-wh%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    The direct answer to your question is that there is no way to find out if you have just started a new line when you use Scanner::next / Scanner::hasNext. And more generally, there is no way to find out what the last delimiter was. The delimiters are discarded.



    As JB Nizet says there are lots of existing open source CSV reader libraries, so there is no need to implement this functionality using Scanner. Indeed, implementing CSV reading properly is not trivial, especially if you need to implement headers, quoting, escaping and/or continuation lines. Using an existing library is advisable.



    But if (against advice!) you decide to implement the reader directly, then a more robust approach is to use a nested loop:




    • The outer loop reads complete lines using nextLine

    • The inner loop creates a Scanner for each line to split it into fields.


    Except that that doesn't deal with quoting, escaping, continuation lines, etc. The real problem is that the CSV grammar doesn't have a simple context independent delimiter.




    I guess I could maybe use some sort of counter to count [fields]




    Yea ... but if some of the lines in your CSV are missing fields (e.g. due to a human error) then counting the fields won't detect this.






    share|improve this answer


























      2














      The direct answer to your question is that there is no way to find out if you have just started a new line when you use Scanner::next / Scanner::hasNext. And more generally, there is no way to find out what the last delimiter was. The delimiters are discarded.



      As JB Nizet says there are lots of existing open source CSV reader libraries, so there is no need to implement this functionality using Scanner. Indeed, implementing CSV reading properly is not trivial, especially if you need to implement headers, quoting, escaping and/or continuation lines. Using an existing library is advisable.



      But if (against advice!) you decide to implement the reader directly, then a more robust approach is to use a nested loop:




      • The outer loop reads complete lines using nextLine

      • The inner loop creates a Scanner for each line to split it into fields.


      Except that that doesn't deal with quoting, escaping, continuation lines, etc. The real problem is that the CSV grammar doesn't have a simple context independent delimiter.




      I guess I could maybe use some sort of counter to count [fields]




      Yea ... but if some of the lines in your CSV are missing fields (e.g. due to a human error) then counting the fields won't detect this.






      share|improve this answer
























        2












        2








        2






        The direct answer to your question is that there is no way to find out if you have just started a new line when you use Scanner::next / Scanner::hasNext. And more generally, there is no way to find out what the last delimiter was. The delimiters are discarded.



        As JB Nizet says there are lots of existing open source CSV reader libraries, so there is no need to implement this functionality using Scanner. Indeed, implementing CSV reading properly is not trivial, especially if you need to implement headers, quoting, escaping and/or continuation lines. Using an existing library is advisable.



        But if (against advice!) you decide to implement the reader directly, then a more robust approach is to use a nested loop:




        • The outer loop reads complete lines using nextLine

        • The inner loop creates a Scanner for each line to split it into fields.


        Except that that doesn't deal with quoting, escaping, continuation lines, etc. The real problem is that the CSV grammar doesn't have a simple context independent delimiter.




        I guess I could maybe use some sort of counter to count [fields]




        Yea ... but if some of the lines in your CSV are missing fields (e.g. due to a human error) then counting the fields won't detect this.






        share|improve this answer












        The direct answer to your question is that there is no way to find out if you have just started a new line when you use Scanner::next / Scanner::hasNext. And more generally, there is no way to find out what the last delimiter was. The delimiters are discarded.



        As JB Nizet says there are lots of existing open source CSV reader libraries, so there is no need to implement this functionality using Scanner. Indeed, implementing CSV reading properly is not trivial, especially if you need to implement headers, quoting, escaping and/or continuation lines. Using an existing library is advisable.



        But if (against advice!) you decide to implement the reader directly, then a more robust approach is to use a nested loop:




        • The outer loop reads complete lines using nextLine

        • The inner loop creates a Scanner for each line to split it into fields.


        Except that that doesn't deal with quoting, escaping, continuation lines, etc. The real problem is that the CSV grammar doesn't have a simple context independent delimiter.




        I guess I could maybe use some sort of counter to count [fields]




        Yea ... but if some of the lines in your CSV are missing fields (e.g. due to a human error) then counting the fields won't detect this.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 27 '18 at 23:16









        Stephen C

        514k69563917




        514k69563917






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951275%2fis-there-a-way-to-check-if-java-has-started-reading-a-new-line-in-a-text-file-wh%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas