Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How can I read a file and store it's objects into my custom linked list?

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How can I read a file and store it's objects into my custom linked list?

    Basically, I am not using any of the Java API classes such as ArrayLists or LinkedLists, as I have created my own custom linked list class which in this case is named FoodList. I am trying to figure out a way to read each line from a file where each line is an object, and store it in FoodList.

    I've looked at many examples of reading files online, but none of them were for a custom linked list.

    HTML Code:
    package lab9;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;
    
    public class Console {
    	
        FoodList x = new FoodList();
    	
    	public Console(){
    		this.x = new FoodList();
    	}
    	
    	public void Display() {
    		
    		System.out.println("============================================================================");
    		System.out.println("Name                Food Group          Calories            Daily percentage   ");
    		System.out.println("============================================================================");
    		
            File f = new File("food.txt");
            
            try {
            	Scanner scan = new Scanner(f);  //What I tried to do to read in objects from file
            	
                while(scan.hasNextLine()){
                    String line = scan.nextLine();
                    String[] details = new String[4];
                    details = line.split(" ");
                    String name = details[0];
                    String group = details[1];
                    int calories = Integer.parseInt(details[2]);
                    double percentage = Double.parseDouble(details[3]);
                    x.add(new Food(name, group, calories, percentage));
                }
                
                System.out.println(x);
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.out.println("Not working");
            }
        }
    	
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How can I read a file and store it's objects into my custom linked list?

    What happens when you compile and execute your program?
    Can you copy the output and post it there so we can see what it is doing?
    What is wrong with what the program is doing?
    What do you want it to do differently?

    Note: x is a poor name for a variable that references a list and f for a file.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. loop through linked list of objects and their fields
    By dulitul in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 2nd, 2013, 07:49 PM
  2. Adding Student Objects into singly linked list alphabetically [NullPointerException]
    By Restivethought in forum Object Oriented Programming
    Replies: 2
    Last Post: March 29th, 2013, 07:37 AM
  3. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 07:52 PM
  4. create linked list from file?
    By junsugal in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: November 13th, 2011, 12:39 AM