Hey guys. I can read the data to a monitor perfectly. But, I'm having problem reading data from an external file into an array of class objects.
Here's my attempt at writing the method:
private void openFile() //This method asks the user to enter a file name(including the file extension) and then //Sets the data to an array of Product type { String fileName, storeName="", emptyLine1="", emptyLine2="", name="", productName=""; int demandRate=0; double setupCost=0.0, unitCost=0.0, inventoryCost=0, sellingPrice=0; Scanner inputStream = null; System.out.println("Please enter the file name: "); fileName = console.next(); try { inputStream = new Scanner(new File(fileName)); } catch(FileNotFoundException e) { System.out.println("The file does not exist."); continueOptionMain(); } while (inputStream.hasNextLine()) { storeName = inputStream.nextLine(); //!!!! emptyLine1 = inputStream.nextLine(); //!!!! name = inputStream.nextLine(); productName = name.substring(7); demandRate = inputStream.nextInt(); setupCost = inputStream.nextDouble(); unitCost = inputStream.nextDouble(); inventoryCost = inputStream.nextDouble(); sellingPrice = inputStream.nextDouble(); emptyLine2 = inputStream.nextLine(); //!!!! if (storeName.equalsIgnoreCase("lambton")) { lambtonStore.addData("lambton", productName, demandRate, setupCost, unitCost, inventoryCost, sellingPrice); } else if(storeName.equalsIgnoreCase("callaghan")) { callaghanStore.addData("callaghan", productName, demandRate, setupCost, unitCost, inventoryCost, sellingPrice); } } inputStream.close(); continueOptionMain(); }
I have put //!!!! next to the lines I believe to be the problem. The reason I think that storeName is a problem is because it is not included between every product. And I'm not entirely sure whether the empty lines are causing a problem or not.
Here is what the example file I have is set out like:
Lambton:
Name: chocolate
demand rate: 50
setup cost: 200
unit cost: 5.2
inventory cost: 2
selling price: 3
Name: cake
demand rate: 50
setup cost: 300
unit cost: 4
inventory cost: 2
selling price: 3.1
Callaghan:
Name: coffee
demand rate: 100
setup cost: 200.5
unit cost: 4
inventory cost: 2.1
selling price: 2
Any advice on what I'm doing wrong would be greatly appreciated