2

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.

9
  • What does your input file look like? Commented May 5, 2015 at 8:50
  • string name string description int price string name string description double price etc Commented May 5, 2015 at 8:51
  • It would be great if you post your input file with 2 parts. Commented May 5, 2015 at 8:52
  • Sorry its automatically formatting my comment onto one line, but each String/Int is on a new line in the document Commented May 5, 2015 at 8:52
  • could you post the exact exception as it is shown? Also how do you know The loop works fine up until the last part needs to be added to the arraylist,? Commented May 5, 2015 at 8:54

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Hey thanks for your help. Iam not sure i understand exactly what your saying though. Is that within the for loop??
I mean that you should use nextLine() for all your parameters (not nextDouble()). That's because when you use nextDouble() the "pointer" of the Scanner is just before the \n, and the next execution of nextLine() reads just the \n next to your double. Hope I've explained it
Yes, but then you can convert it to a double with: double price = Double.parseDouble(lineString);
1

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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.