Srk93
Srk93

Reputation: 37

Creating Linked List from Contents of a File?

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:

 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()<=30 && groceryList.length()>=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");
    }

This is the content for both files:

meals.txt

1 bread

2 egg

1 orange juice

2 bread

1 peanut butter

1 jam

1 apple

1 spaghetti

1 tomato sauce

1 bread

2 egg

1 orange juice

2 bread

1 peanut butter

1 jam

1 peach

1 chicken

1 rice

1 green beans

inventory.txt

1 tomato sauce

2 peach

1 apple

3 bread

1 jam

1 pear

Upvotes: 0

Views: 376

Answers (1)

Prasad Karunagoda
Prasad Karunagoda

Reputation: 2148

Below program demonstrates how file content can be added to a LinkedList.

import java.io.*;
import java.util.*;

public class UsingLinkedList
{
  public static void main(String[] args)
  {
    System.out.println("Enter meal plan file name :");
    Scanner input = new Scanner(System.in);
    String mealList = input.nextLine().trim();

    List<String> meals = new LinkedList<>();
    try
    {
      File inp1 = new File(mealList + ".txt");
      Scanner inputfile = new Scanner(inp1);
      while (inputfile.hasNextLine())
      {
        meals.add(inputfile.nextLine());
      }
      System.out.println(meals);
    }
    catch (FileNotFoundException exception)
    {
      exception.printStackTrace();
      System.out.println("File not found. Please re-enter the file name.");
    }
  }
}

For below input file content,

Meal plan 1
Meal plan 2
Meal plan 3
Meal plan 4

Output is:

Enter meal plan file name :
MealPlans
[Meal plan 1, Meal plan 2, Meal plan 3, Meal plan 4]

Upvotes: 3

Related Questions