Search code examples
javaiolinked-listnosuchelementexception

How to read from file and store the information in a Linked List (Java)?


I'm trying to read a file that contains attributes that create a CellPhone (such as serial number, brand, year, and price). Then, I want to store the information in variables so that I can create CellPhone objects using its constructor. Afterwards I need to keep adding these CellPhone objects in a Linked List, all the while making sure that there are no duplicates (CellPhone objects with the same serial number). It works properly for the first case where the List is empty, however after I've added the first object to the List, there is a NoSuchElementException. What have I done wrong and how can I properly read the file? Any help is appreciated.

CellListUtilization Class:

// Method to read the file and store the information in the CellList
public static void processFile(Scanner sc1, CellList cl1) {
    String S = null;

    while(sc1.hasNext())
    {
        // First case where the list is empty
        if (cl1.getSize() == 0)
        {
            S = sc1.next();
            serialNum = Long.parseLong(S);
            S = sc1.next();
            brand = S;
            S = sc1.next();
            price = Double.parseDouble(S);
            S = sc1.next();
            year = Integer.parseInt(S);

            CellPhone c1 = new CellPhone(serialNum, brand, year, price);
            cl1.addToStart(c1);
        }
        else
        {
            serialNum = Long.parseLong(S);
            S = sc1.next();
            brand = S;
            S = sc1.next();
            price = Double.parseDouble(S);
            S = sc1.next();
            year = Integer.parseInt(S);

            if (!(cl1.contains(serialNum)))
            {
                CellPhone c2 = new CellPhone(serialNum, brand, year, price);
                cl1.addToStart(c2);
            }
        }
        S = sc1.next();
    }
}

The file I'm trying to read:

3890909 Samsung         857.28 2015
2787985 Acer            572.20 2013
4900088 LG              232.99 2017
1989000 Nokia           237.24 2006
0089076 Sharp           564.22 2009
2887685 Motorola        569.28 2012
7559090 Pansonic        290.90 2005
2887460 Siemens         457.28 2009
2887685 Apple           969.28 2018
6699001 Lenovo          237.29 2012
9675654 Nokia           388.00 2009
1119002 Motorola        457.28 2008
5000882 Apple           977.27 2016
8888902 Samsung         810.35 2018
5890779 Motorola        457.28 2007
7333403 BenQ            659.00 2009
2999900 Siemens         457.28 2006
6987612 HTC             577.25 2009
8888902 BenQ            410.35 2009
8006832 Motorola        423.22 2008
5555902 SonyEricsson    177.11 2007
9873330 Nokia           677.90 2010
8888902 BenQ            410.35 2009
5909887 Apple           726.99 2017
2389076 BlackBerry      564.22 2010
1119000 SonyEricsson    347.94 2009

Solution

  • Your code is trying to reach for an element on the next line before the while loop condition can assert that the line exists. Should the file have 4 columns of data in a line, you should not call sc1.next() more than 4 times in a single loop to avoid NoSuchElementException.

    Moving the last sc1.next() call from the end of while loop to the beggining of the else block should fix the issue.

    while(sc1.hasNext())
    {
        // First case where the list is empty
        if (cl1.getSize() == 0)
        {
            S = sc1.next();
            serialNum = Long.parseLong(S);
            S = sc1.next();
            brand = S;
            S = sc1.next();
            price = Double.parseDouble(S);
            S = sc1.next();
            year = Integer.parseInt(S);
    
            CellPhone c1 = new CellPhone(serialNum, brand, year, price);
            cl1.addToStart(c1);
        }
        else
        {
            S = sc1.next();
            serialNum = Long.parseLong(S);
            S = sc1.next();
            brand = S;
            S = sc1.next();
            price = Double.parseDouble(S);
            S = sc1.next();
            year = Integer.parseInt(S);
    
            if (!(cl1.contains(serialNum)))
            {
                CellPhone c2 = new CellPhone(serialNum, brand, year, price);
                cl1.addToStart(c2);
            }
        }
    }