I have a .txt file and require its contents to be put into an arraylist. It is in the following format, Each element(int,String) being on a new line within the document.
int number of parts string partname string description int price string partname string description int price etc.
Whenever i run my program it will add the first however many attributes, but will not add the last three attributes to the arraylist.
public static void loadPartsCleanly(){
try {
// java.io.File file = new java.io.File("parts.txt");
Scanner in = new Scanner(new File("parts.txt"));
ArrayList<Part> partlist = new ArrayList<Part>();
String name=null;
String description = null;
double price =0.0;
int totalParts = 0;
totalParts = in.nextInt();
in.nextLine();
//totalParts ++ ;
System.out.println(totalParts);
for (int i = 0; i < totalParts; i++)
{
//ArrayList<Part> partlist = new ArrayList<Part>();
name = in.nextLine();
description = in.nextLine();
price = in.nextDouble();
in.nextLine();
int quantityInStock = 5;
partlist.add(new Part(name, description, price, quantityInStock));
System.out.println(partlist);
}
in.close();
} catch (FileNotFoundException fnfe) {
System.out.println("Unable to locate the parts.txt file for opening.");
} catch (Exception otherExc) {
System.out.println("***** An unexpected error has occurred *****");
otherExc.printStackTrace();
}
}
so in the above code it reads the first Int in the text document and assigns it for use in the for loop.
totalParts = in.nextInt();
in.nextLine();
//totalParts ++ ;
System.out.println(totalParts);
for (int i = 0; i < totalParts; i++)
The loop works fine up until the last part needs to be added to the arraylist, regardless of whether totalParts is 8 or 20.
Which gives this error..
An unexpected error has occurred java.util.NoSuchElementException: No line found
I have been trying to figure this out but increasing frustration has prompted me to post on here, so any pointers in the right direction would be greatly appreciated. If you need clarification with anything regarding my question, please ask.