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?