MrKeyboard
MrKeyboard

Reputation: 107

Java - reading .txt file to arrayList omits last iteration and shows error

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.

Upvotes: 2

Views: 133

Answers (2)

dmananes
dmananes

Reputation: 104

The nextInt() and nextDouble() methods are different than nextLine(). nextLine() reads the \n but the others.

So, if you have each element in a different line, you should use nextLine() always and then parse that line to the type that you want, for example:

String line = in.nextLine();
double price = Double.parseDouble(line);

Upvotes: 2

Howard Wang
Howard Wang

Reputation: 601

I think the last line of your input file is a number. What I saw from your code is that you use nextDouble to read the price and then you use nextLine to go to next line. In such situation, if there is no more line behind the last number, you got error.

The following code solves your problem.

if (i + 1 < totalParts) {
    in.nextLine();
}

Upvotes: 1

Related Questions