I'm trying to create a program that'll read a list from an external file, and generate an arrayList with the object representation of the list. Basically, the list contains names, and prices, which will be read in, and used to create instances of my "book" class which are supposed to be all stored in an array list. However the end result is that all the elements of the list array end up being copies of the last element, even though I know that file is correct, and the string list that is created before the book list is correct, as well as the book objects I try to pass in. It's when I try to get it back out that I have trouble.

Is there a particular nuisance I'm simply missing?

/**
* List building class
**/

package Library;

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

public class BookTracker
{
	static ArrayList<String> input = new ArrayList<String>();
	static ArrayList<LibraryBook> bookList = new ArrayList<LibraryBook>();
	static ArrayList<LibraryBook> booksOut = new ArrayList<LibraryBook>();
	static BufferedReader src;
	
	public static void main(String[] args)
	{
		
		if(findFile())
		{
			if(loadStringArray())
			{
				loadBookArray();
			}
		}
		for(LibraryBook lb : bookList)
		{
			System.out.println(lb.toString());
		}
		/*for(String str : input)
		{
			System.out.println(str);
		}*/
	}
	
	static boolean findFile()
	{
		try
		{
			src = new BufferedReader(new FileReader("./Library/bookCatalog.txt"));
		}
		catch(FileNotFoundException e)
		{
			System.out.println("error");
			return false;
		}
		
		return true;
	}

	static boolean loadStringArray()
	{
		boolean passback = false;
		try
			{
				//if there are any strings to be written this
				//will be switched, else the program won't
				//waste time dealing with a empty array list
				String str;
				while((str = src.readLine()) != null)
				{
					passback = true;
					input.add(str);
				}
				src.close();
			}
		catch(IOException e)
		{
			System.out.println("error 2");
		}
		return passback;	
	}
	
	static void loadBookArray()
	{
		String[] book;
		for(String str : input)
		{
			book = str.split(";", 3);
			//System.out.print(book[0]);
			//System.out.print(book[1]);
			//System.out.println(book[2]);
			LibraryBook lb = new LibraryBook(book[0],
				new Double(book[1]), new Double(book[2]));
			System.out.println(lb.toString());
			bookList.add(lb);
		}
	}

}

/**
* Book class
**/

package Library;

public class LibraryBook
{
	static String name;
	static double perDayCharge;
	static double maximumCharge;
	
	public LibraryBook(String name, double perDayCharge, double maximumCharge)
	{
		this.name = name;
		this.perDayCharge = perDayCharge;
		this.maximumCharge = maximumCharge;
	}
	
	public String toString()
	{
		String passback;
		passback = name;
		passback += ", " + Double.toString(perDayCharge);
		passback += ", " + Double.toString(maximumCharge);
		return passback;
	}
	
	public static double calculateCharge()
	{
		double passback = 0;
		return passback;
	}
}

/**
* book list
**/


tada;0.15;5.00
brownie;0.10;4.50
gravy;0.25;10.00
lemon;0.50;11.00
sausage;0.50;9.50
chicken;0.25;5.25

/**
* Output
**/

//this is from the string list
tada, 0.15, 5.0
brownie, 0.1, 4.5
gravy, 0.25, 10.0
lemon, 0.5, 11.0
sausage, 0.5, 9.5
chicken, 0.25, 5.25

//This is what comes out
chicken, 0.25, 5.25
chicken, 0.25, 5.25
chicken, 0.25, 5.25
chicken, 0.25, 5.25
chicken, 0.25, 5.25
chicken, 0.25, 5.25

Edited 5 Years Ago by WigglesMcMuffin: n/a

static String name;
static double perDayCharge;
static double maximumCharge;

static means that there is only one value for this variable, and all instances of the class share that same single value. I bet that's not what you intended!

Wow... I could smack myself, those are the little things I should be catching now a days. So simple! Thank you very much! Works like a charm now.

This question has already been answered. Start a new discussion instead.