-2

The code shown below should read each line individually from a text file, store the current line into the String fileLine and then print out the contents of fileLine

When the text file only contains one line, the code executes as described prints the line. However when the text file has more than one line, the 'while(sc.hasNextLine())' statement is skipped completely, which has left me scratching my head as I use a similar version of this code to read from other text files within the same project.

Code

private File stockFile = new File(stockFilePath);

public void fetchCurrentStock() throws FileNotFoundException {
        Scanner sc = new Scanner(stockFile);
        String fileLine = "";
        //Name\tPrice\tDescription\tSupplierName\tItemImagePath\tItem_ID\tQuantity
        while(sc.hasNextLine()){
            fileLine = sc.nextLine();
            System.out.println(fileLine);
        }
   }

Text File Contents

Samsung Galaxy S20+ 999 Samsung's latest & greatest PhonesHub   null    001 20
Samsung Galaxy A10  130 Capture clear photos, in any light. Watch, play and browse more on the 6.2 inch HD+ display.    PhonesHub   null    002 25
Samsung Galaxy S10  750 Last years base flagship model, great power at a respectable price. PhonesHub   null    003 30
Huawei P30      380 Take your photography to the next level with the P30’s triple camera & 6.1 inch display.    PhonesHub   null    004 15

Update: It seems that when there is less text per line in the text file the Scanner.nextLine() functions as intended. Perhaps using the Scanner class is not an option here?

Dylan
  • 43
  • 6
  • 3
    Not related, but it will probably be easier using [String#split](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)) – Juan Mendes Mar 31 '20 at 21:14
  • 2
    This is hardly an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example): Nearly all of the code posted, especially the "parsing" code, has nothing whatsoever to do with why your scanner is not finding all lines. Even the sample input file does not need all that contents to create an MCVE. Please edit your code and data down to the absolute minimum required to demonstrate the problem. – Bohemian Mar 31 '20 at 21:20
  • @JuanMendes I have heard this but haven't used it. I'll have a look at that, thanks! – Dylan Mar 31 '20 at 21:21
  • 1
    **Unable to reproduce.** Copied code and text file from [**version 5**](https://stackoverflow.com/revisions/60959952/5) *(dated 2020-03-31 22:22:12Z)* of the question. Program printed 4 lines of text, as expected. --- If your program doesn't print anything, did you remember to actually *call* method `fetchCurrentStock()`? – Andreas Mar 31 '20 at 23:34
  • @Andreas yes the method is being called. As stated, when I delete all but *one* line from the text file, the program prints the value to console. Also when I remove a large portion of the text from each line, the program will print out all the lines in the text file. – Dylan Mar 31 '20 at 23:47
  • Maybe whatever you're printing it on can't handle long lines. – user207421 Mar 31 '20 at 23:49
  • 1
    Oh, I think I know what is going on. Are you running the program in Eclipse? The Eclipse `Console` view, where the program output shows, has a problem showing very long lines. Your problem is not your program, but Eclipse. **Try running your program from a command prompt.** See e.g. [Eclipse console doesn't show the whole output](https://stackoverflow.com/a/9312989/5221149) or [Printing very big BigIntegers](https://stackoverflow.com/a/11173979/5221149) – Andreas Mar 31 '20 at 23:55
  • @Andreas I'm using IntelliJ **however** I just imported the whole project into Eclipse and it executes completely fine. I'll need to do some more digging however thank you for drilling into my head that the code functions properly, swapping IDE will work as a bandaid solution. – Dylan Apr 01 '20 at 00:12
  • 1
    @Dylan IntelliJ likely has a similar max line length issue as Eclipse, just hits with shorter lines than Eclipse. It likely has a setting for changing that limit, similar to how Eclipse does, as shown in the links provided. – Andreas Apr 01 '20 at 00:19

1 Answers1

0

This seems to be an issue with either my current version (or more likely, my configuration) of the IntelliJ IDE. When running the debug tool to investigate, the while loop will instantly return false.

After importing the project into Eclipse, the code functioned as intended. I can also build my IntelliJ version of the project into a JAR and it will function as intended there, so this will be my workaround at present.

Dylan
  • 43
  • 6