简体   繁体   中英

Linked List program only adding one object when reading line from text file

I am having a problem here. I am trying to add more than one line when reading from text file and adding item to linked list, but for some reason it stops after the first line. This program reads a file line by line splits each word into an array and adds that object to a linked list. When I print it only prints the first line of the text file.

package com.foodlist;

public class Food {
private String name;
private String group;
private int calories;
private double percentage;

public Food(String name, String group, int calories, double percentage){
    this.name = name;
    this.group = group;
    this.calories = calories;
    this.percentage = percentage;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getGroup() {
    return group;
}

public void setGroup(String group) {
    this.group = group;
}

public int getCalories() {
    return calories;
}

public void setCalories(int calories) {
    this.calories = calories;
}

public double getPercentage() {
    return percentage;
}

public void setPercentage(double percentage) {
    this.percentage = percentage;
}

public String toString(){
    String result = "";

    result = name + " " + group + " " + calories + " " + percentage + "\n";

    return result;
}

}public class FoodList {

    private FoodListNode head;

    private class FoodListNode{
        public Food f;
        public FoodListNode next;
        public FoodListNode(Food f){
            this.f = f;
            this.next = null;
        }
    }
        public FoodList(){
            head = null;
        }
        public FoodList getFoodList(){
        final String FILE_NAME = "foods.txt";
        String[] item = null;
        Scanner inFile = null;
        try {
            inFile = new Scanner(new File(FILE_NAME));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
FoodList list = new FoodList();

            while (inFile.hasNextLine()){
            String line = inFile.nextLine().trim();
            item = line.split("\\s+");
            if(item.length==4){
            list.add(new Food(item[0], item[1], Integer.parseInt(item[2]), Double.parseDouble(item[3])));
            }
            }
        inFile.close();
        return list;
        }

        public void add(Food f){
            FoodListNode node = new FoodListNode(f);
            if (head == null){
                head = node;
            }
                else{
                    FoodListNode tmp = head;
                    while (tmp.next != null){
                        tmp = tmp.next;
                        tmp.next = node;
                    }
                }
                }
            public String toString(){
                String result = "";

                FoodListNode tmp;

                for(tmp = head; tmp != null; tmp = tmp.next){
                    result += tmp.f;
                }
            return result;  
            }
        }
public class FoodMenu {


    public static void main(String[] args) {
        FoodList items = new FoodList();

        FoodList list = items.getFoodList();

        System.out.println(list);
}
}

The problem was the braces I put in the while loop here:

if (head == null){
    head = node;
} else {
    FoodListNode tmp = head;
    while (tmp.next != null)->{
        tmp = tmp.next;
        tmp.next = node;
    }<-
}

I removed them and now it is working perfectly. Have not looked into why this change fixed it, but will look into it more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM