title: Creating Linked List from Contents of a File?
body: <p>I'm trying to create a grocery list based on ingredients and inventory. The program is supposed to ask the user for a file that has the ingredients that are needed (meal plan file), then it asks the user for a file of ingredient inventory, and then it creates a file that is supposed to print out the ingredients needed (so the ingredients that are not in the inventory file but are listed in the meal plan file. So each file must have the quantity and the food item separated by exactly one space and there must be an error prompt if there is a problem in a line in the file. Anyways I am just wondering how I can do this using Linked Lists? How can I create a linked list based on the contents of the file so I can print out the grocery list onto another file for the user? This is my code so far:</p>
  
  <pre><code> public static void main (String [] args)
  {
  
      Scanner input = new Scanner(System.in);
      String mealList, inventoryList, groceryList;
  
      System.out.println("Enter meal plan file name :");
      mealList = input.nextLine().trim();
  
      try
      {
          File inp1 = new File(mealList + ".txt");
          Scanner inputfile = new Scanner(inp1);
      }
      catch (FileNotFoundException exception)
      {
          System.out.println("File not found. Please re-enter the file name.");
      }
      catch (IOException exception)
      { 
          System.out.println(exception);
      }
  
      System.out.println("Enter inventory file name :");
      inventoryList = input.nextLine().trim();
      try
      {
          File inp2 = new File(inventoryList + ".txt");
          Scanner inputfile2 = new Scanner(inp2);
      }
      catch (FileNotFoundException exception)
      {
          System.out.println("File not found. Please re-enter the file name.");
      }
      catch (IOException exception)
      { 
          System.out.println(exception);
      }
  
      System.out.println("Enter name of grocery list file to create :");
      groceryList = input.nextLine().trim();
  
      try
      {
          if (groceryList.length()&lt;=30 &amp;&amp; groceryList.length()&gt;=1)
          {
  
              groceryList = groceryList + ".txt";
  
              File file = new File(groceryList);
              file.createNewFile();
          }
          else
          {
              System.out.println("Wrong length");
          }
      }
      catch (IOException exception)
      {
          System.out.println("Invalid Input. Please re-enter the file name");
      }
  </code></pre>
  
  <p>This is the content for both files:</p>
  
  <p>meals.txt</p>
  
  <p>1 bread</p>
  
  <p>2 egg</p>
  
  <p>1 orange juice</p>
  
  <p>2 bread</p>
  
  <p>1 peanut butter</p>
  
  <p>1 jam</p>
  
  <p>1 apple</p>
  
  <p>1 spaghetti</p>
  
  <p>1 tomato sauce</p>
  
  <p>1 bread</p>
  
  <p>2 egg</p>
  
  <p>1 orange juice</p>
  
  <p>2 bread</p>
  
  <p>1 peanut butter</p>
  
  <p>1 jam</p>
  
  <p>1 peach</p>
  
  <p>1 chicken</p>
  
  <p>1 rice</p>
  
  <p>1 green beans</p>
  
  <p>inventory.txt</p>
  
  <p>1 tomato sauce</p>
  
  <p>2 peach</p>
  
  <p>1 apple</p>
  
  <p>3 bread</p>
  
  <p>1 jam</p>
  
  <p>1 pear</p>