Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to input data into an arraylist from a text file?

807606Apr 25 2007 — edited Apr 25 2007
I am trying to take data from a text file and put that data into an arraylist. First here is the text file:
[item1, 10, 125.0, item2, 10, 12.0, item3, 20, 158.0]
3
4530.0
[item5, 65, 555.5, item4, 29, 689.0]
2
56088.5
[item7, 84, 34.5, item6, 103, 0.5, item8, 85, 1.36]
3
3065.1
The text between the [ ] is the output from my arraylists. I have three arraylists. The first [ ] belongs to arraylist A, the second to arraylist B, and the third to arraylist C. The format of the arraylists is this:

<item name>,<# in stock>,<value of one item>

The first number below the arraylists in the text file represents the number of items in the list. The second number below the arraylists represents the total value of the items in the arraylists.

Here is the class file I have (yes, it does everything):
 import java.io.*;
 import java.util.Scanner;
 import java.util.ArrayList;
 

 public class Inventory extends Object
 {
 	public int toAdd = 0;
 	private boolean done = false;			//Are we done yet?
 	
 	public String strItemName;				//The name of the item type.
 	public int intNumInStock;				//The number in stock of that type. 	
 	public double dblValueOfOneItem;		//The value of one item.
    public String strNumberInStock;			
    
    public double dblTotalValueA;			//The total value of warehouse A.
    public double dblTotalValueB;			//The total value of warehouse B.
    public double dblTotalValueC;			//The total value of warehouse C.
    
    public int intWarehouseAItemCount;		//Counter for items in warehouse A.
    public int intWarehouseBItemCount;		//Counter for items in warehouse B.
    public int intWarehouseCItemCount;		//Counter for items in warehouse C.
    
 	ArrayList warehouseAList = new ArrayList();	//Create the Warehouse A ArrayList.							
 	ArrayList warehouseBList = new ArrayList(); //Create the Warehouse B arrayList.
 	ArrayList warehouseCList = new ArrayList(); //Create the Warehouse C arrayList.
 
 	/** Construct a new Inventory object. */
 	public Inventory()
 	{
 		super();
 	}
 	
 	/**Add items to the Warehouse A ArrayList.*/
 	private void createWareHouseA()
 	{
 		System.out.println("!" + toAdd + " item types will be added to warehouse A.");
 		
 		//Cast
 		String strNumInStock = Integer.toString(intNumInStock);
 		String strValueOfOneItem = Double.toString(dblValueOfOneItem);
 		
 		this.strNumberInStock = strNumInStock;
 		
 		System.out.println("!Initial size of warehouseAList :  " + 
 												warehouseAList.size());
 		
 		//Add items to the array List
 		warehouseAList.add(this.strItemName);
 		warehouseAList.add(this.strNumberInStock);
 		warehouseAList.add(this.dblValueOfOneItem);
 		
 		System.out.println("!size of arrayList after additions " + warehouseAList.size());
 		
 	}
 	
 	/**Add items to the Warehouse B ArrayList.*/
 	private void createWareHouseB()
 	{
 		System.out.println("!" + toAdd + " item types will be added to warehouse B.");
 		
 		//Cast
 		String strNumInStock = Integer.toString(intNumInStock);
 		String strValueOfOneItem = Double.toString(dblValueOfOneItem);
 		
 		this.strNumberInStock = strNumInStock;
 		
 		System.out.println("!Initial size of warehouseBList :  " + 
 												warehouseBList.size());
 		
 		//Add items to the array List
 		warehouseBList.add(this.strItemName);
 		warehouseBList.add(this.strNumberInStock);
 		warehouseBList.add(this.dblValueOfOneItem);
 		
 		System.out.println("!size of arrayList after additions " + warehouseBList.size());
 		
 	}
 	
 	/**Add items to the Warehouse C ArrayList.*/
 	private void createWareHouseC()
 	{
 		System.out.println("!" + toAdd + " item types will be added to warehouse C.");
 		
 		//Cast
 		String strNumInStock = Integer.toString(intNumInStock);
 		String strValueOfOneItem = Double.toString(dblValueOfOneItem);
 		
 		this.strNumberInStock = strNumInStock;
 		
 		System.out.println("!Initial size of warehouseCList :  " + 
 												warehouseCList.size());
 												
 		//Add items to the array List
 		warehouseCList.add(this.strItemName);
 		warehouseCList.add(this.strNumberInStock);
 		warehouseCList.add(this.dblValueOfOneItem);
 		
 		System.out.println("!size of arrayList after additions " + warehouseCList.size());
 		
 	}

 	/**Interpret the commands entered by the user.*/
 	public void cmdInterpreter()
 	{
 		this.displayHelp();
 		Scanner cin = new Scanner(System.in);
 		while (!this.done)
 		{
 			System.out.print(">");
 			//"line" equals the next line of input.
 			String line = cin.nextLine();
 			this.executeCmd(line);
 		}
 	}
 	
 	
 	/**Execute one line entered by the user. 
 	 * @param cmdLN; The command entered by the user to execute. */
 	 private void executeCmd(String cmdLN)
 	 {
 	 	Scanner line = new Scanner(cmdLN);
 	 	if (line.hasNext())
 	 	{
 	 		String cmd = line.next();
 	 		
 	 		//What to do when users enter the various commands below.
 	 		if (cmd.equals("help"))
 	 		{
 	 			this.displayHelp();
 	 		}
 	 		else if (cmd.equals("Q!"))
 	 		{
 	 			this.done = true;
 	 		}
 	 		else if (cmd.equals("F") && line.hasNext())
 	 		{
 	 			this.inputFromFile(line.next());
 	 		}
 	 		else if (cmd.equals("K") && line.hasNext())
 	 		{
 	 			int numItemsToAdd = Integer.valueOf( line.next() ).intValue();
 	 			this.toAdd = numItemsToAdd;
 	 			this.inputFromKeyboard(numItemsToAdd);
 	 		}
 	 	}
 	 }
 	 
 	 /**What to do if input comes from a file. 
 	  *	@param inputFile; The name of the input file to process.*/
 	 private void inputFromFile(String inputFile)
 	 {
 	 	Scanner cin = new Scanner(System.in);
 	 	
 	 	System.out.println("!Using input file " + inputFile + ".");
 	 	
 	 	System.out.print("!Enter the name of the output file: ");
 	 	String outputFile = cin.next();
 	 	
 	 	System.out.println("!Using output file " + outputFile + ".");
	
		try
		{
			BufferedReader in = new BufferedReader(new FileReader(inputFile));
			//Scanner in = new Scanner(new File(inputFile));
	
			//Initialize the String variables.
			String line1 = null;
			String line2 = null;
			String line3 = null;
			String line4 = null;
			String line5 = null;
			String line6 = null;
			String line7 = null;
			String line8 = null;
			String line9 = null;
			
			System.out.println(in.equals(",") + " see?");
			
			
			//System.out.println((char)(char)in.read() + " experiment");
			
			/**This loop assigns values to the string variables based on the 
			 * values on each line in the input file. */
			while(in.readLine() != null)
			{
				
				line1 = in.readLine();
				line2 = in.readLine();
				line3 = in.readLine();
				line4 = in.readLine();
				line5 = in.readLine();
				line6 = in.readLine();
				line7 = in.readLine();
				line8 = in.readLine();
				line9 = in.readLine();
			}
		
			//Print the contents of each line in the input file.
			System.out.println("!value of line 1: " + line1);
			System.out.println("!value of line 2: " + line2);
			System.out.println("!value of line 3: " + line3);
			System.out.println("!value of line 4: " + line4);
			System.out.println("!value of line 5: " + line5);
			System.out.println("!value of line 6: " + line6);
			System.out.println("!value of line 7: " + line7);
			System.out.println("!value of line 8: " + line8);
			System.out.println("!value of line 9: " + line9);
			
		  /**Add items to the warehouses.*/
			warehouseAList.add(line1);
			warehouseBList.add(line4);
			warehouseCList.add(line7);
			
		  /**Add the item count and total value for warehouse A.*/
			int intLine2 = Integer.valueOf(line2).intValue();
			this.intWarehouseAItemCount = intLine2;
			double dblLine3 = Double.valueOf(line3).doubleValue();
			this.dblTotalValueA = dblLine3;
			
		  /**Add the item count and total value for warehouse B.*/
			int intLine5 = Integer.valueOf(line5).intValue();
			this.intWarehouseBItemCount = intLine5;
			double dblLine6 = Double.valueOf(line6).doubleValue();
			this.dblTotalValueB = dblLine6;
			
		  /**Add the item count and total value for warehouse C.*/
		    int intLine8 = Integer.valueOf(line8).intValue();
		    this.intWarehouseCItemCount = intLine8;
		    double dblLine9 = Double.valueOf(line9).doubleValue();
		    this.dblTotalValueC = dblLine9;
			
		  /**Ask the user how many items to add or delete from inventory.*/
		    System.out.print("Enter the number to add to inventory for " +
		    									warehouseAList.get(0) + ":");
		    String toAddOrDel = cin.next();
			
		  /**Print the contents of all the warehouses. */
			System.out.println(" ");
			//Print the contents of warehouse A.
			System.out.println("!--------------------------------");
			System.out.println("!----------Warehouse A:----------");
			System.out.println("!--------------------------------");
			//Print the item list for warehouse A.
			System.out.println(warehouseAList);
			//Print the total amount of items in warehouse A.
			System.out.println("Total items: " + this.intWarehouseAItemCount);
			//Print the total value of the items in warehouse A.
			System.out.println("Total value: " + this.dblTotalValueA);
			
			System.out.println("!--------------------------------");
			System.out.println("!----------Warehouse B:----------");
			System.out.println("!--------------------------------");
			//Print the item list for warehouse B.
			System.out.println("!warehouseB: " + warehouseBList);
			//Print the total amount of items in warehouse B.
			System.out.println("Total items: " + this.intWarehouseBItemCount);
			//Print the total value of the items in warehouse B.
			System.out.println("Total value: " + this.dblTotalValueB);
			
			System.out.println("!--------------------------------");
			System.out.println("!----------Warehouse C:----------");
			System.out.println("!--------------------------------");
			//Print the item list for warehouse C.
			System.out.println("!warehouseC: " + warehouseCList);
			//Print the total amount of items in warehouse C.
			System.out.println("Total items: " + this.intWarehouseCItemCount);
			//Print the total value of the items in warehouse C.
			System.out.println("Total value: " + this.dblTotalValueC);
			
			
			in.close();
		}
		catch (FileNotFoundException e)
		{
			System.out.println("!Error: Unable to open file for reading.");
		}
		/**
		catch (EOFException e)
		{
			System.out.println("!Error: EOF encountered, file may be corrupted.");
		}
		*/
		catch (IOException e)
		{	
			System.out.println("!Error: Cannot read from file.");
		}
 	 	
 	 }
 	 
 	 
 	 /**What to do if input comes from the keyboard. 
 	  *	@param numItems; The total number of items that will be added to the 
 	  * 				 Warehouse(s). */
 	 public void inputFromKeyboard(int numItems)
 	 {
 	 	System.out.println("!You will be adding " + numItems + " items to " +
 	 							"inventory from the keyboard. ");
 	 							
 	 	this.toAdd = numItems;
 	 	
 	 	Scanner cin = new Scanner(System.in);
 	 	
 	 	//Prompt user for name of output file.
 	 	System.out.print("!Enter the name of the output file: ");
 	 	String outputFile = cin.next();
 	 	
 	 
 	 	/**This loop asks the user for information about the item(s) and inputs
 	 	  *them into the appropriate array.*/
 	 	int count = 0;
 	 	while (numItems > count)
 	 	{
 		 	//Item name.
 		 	System.out.print("!Item name: ");
 		 	String addItemName = cin.next();
 		 	//Number in stock.
 		 	System.out.print("!Number in stock: ");
 		 	String addNumInStock = cin.next();
 		 	//Initial warehouse.
 		 	System.out.print("!Initial warehouse(A,B,C): ");
 		 	String addInitWarehouse = cin.next();
 		 	//Value of one item.
 		 	System.out.print("!Value of one item: ");
 		 	String addValueOfOneItem = cin.next();
 		 	
 		 	//Add or delete from inventory
 		 	System.out.print("!Enter amount to add or delete from inventory: ");
 		 	String strAddOrDelete = cin.next();
 		 	
 		 	System.out.println("!Amount to add or delete: " + strAddOrDelete);
 		 	
 		 	//Cast
 		 	int intAddNumInStock = Integer.valueOf(addNumInStock).intValue();
 		 	double doubleAddValueOfOneItem = Double.valueOf(addValueOfOneItem).doubleValue();
 		 	int intAddOrDelete = Integer.valueOf(strAddOrDelete).intValue();
 		 	
 		 	
 		 	/**Add intAddNumInStock with intAddOrDelete to determine the amount
 		 	 * to add or delete from inventory (If a user wishes to remove items
 		 	 * from inventory simply add negative values). */
 		 	intAddNumInStock = intAddNumInStock + intAddOrDelete;
 		 	System.out.println("!Inventory after modifications: " + strAddOrDelete);
 		 	
 		 
 		 	this.strItemName = addItemName;
 		 	this.intNumInStock = intAddNumInStock;
 		 	this.dblValueOfOneItem = doubleAddValueOfOneItem;
 		 	
 		 		
 		 	//Put items into warehouse A if appropriate.
 		 	if (intAddNumInStock < 25)
    	 	{
    	 		//Increment the warehouse A item count.
    	 		this.intWarehouseAItemCount = this.intWarehouseAItemCount + 1;
    	 		
    	 		//Calculate the total value of warehouse A.
    	 		this.dblTotalValueA = this.dblTotalValueA + intAddNumInStock * doubleAddValueOfOneItem;
    	 		
    	 		//Create the warehouse A array list.
 				this.createWareHouseA();

 		 	}
 		 	//Put items into warehouse B if appropriate.
 		 	if (intAddNumInStock >= 25)
    	 	{
    	 		if (intAddNumInStock < 75)
    	 		{
    	 			//Increment the warehouse B item count.
    	 			this.intWarehouseBItemCount = this.intWarehouseBItemCount + 1;
    	 			
    	 			//Calculate the total value of warehouse B.
    	 			this.dblTotalValueB = this.dblTotalValueB + intAddNumInStock * doubleAddValueOfOneItem;
    	 		    	 			
    	 			//Create the warehouse B array list.
    	 			this.createWareHouseB();
    	 		}
 		 	}
 		 	//Put items into warehouse C if appropriate.
 		 	if (intAddNumInStock >= 75)
    	 	{
    	 		//Increment the warehouse C item count.
    	 		this.intWarehouseCItemCount = this.intWarehouseCItemCount + 1;
    	 		
    	 		//Calculate the total value of warehouse C.
    	 		this.dblTotalValueC = this.dblTotalValueC + intAddNumInStock * doubleAddValueOfOneItem;
    	 		
    	 		//Create the warehouse C array list.
 				this.createWareHouseC();

 		 	}
 	 	 	 	 	 	
 	 	 	//display helpful information. 	
 		 	System.out.println("!--------------------------------");
 		 	System.out.println("!" + addItemName + " is the item name.");
 		 	System.out.println("!" + addNumInStock + " is the number in stock.");
 		 	System.out.println("!" + addInitWarehouse + " is the initial warehouse.");
 		 	System.out.println("!" + addValueOfOneItem + " is the value of one item.");
 		 	System.out.println("!--------------------------------------------------");
 			//Increment the counters.
 		 	count++;
 		 }
 		 
 		 /**Create and write to the output file. */
 		 try
 	 	 {
 	 	 	//Use the output file specified by the user.
 	 		PrintWriter out = new PrintWriter(outputFile);
 	 		
 	 	  /**Write warehouse A details.*/
 	 	  	//Blank the first line.
 	 	  	out.println(" ");
 	 		//Write the array list for warehouse A.
 	 		out.println(warehouseAList);
 	 		//Write the amount of items in warehouse A.
 	 		out.println(intWarehouseAItemCount);
 	 		//Write the total value for warehouse A.
 	 		out.println(dblTotalValueA);
 	 		
 	 	  /**Write warehosue B details.*/
 	 	    //Write the array list for warehouse B.
 	 		out.println(warehouseBList);
 	 		//Write the amount of items in warehouse B.
 	 		out.println(intWarehouseBItemCount);
 	 		//Write the total value for warehouse B.
 	 		out.println(dblTotalValueB);
 	 		
 	 	  /**Write warehouse C details.*/
 	 	  	//Write the array list for warehouse C.
 	 		out.println(warehouseCList);
 	 		//Write the amount of items in warehouse C.
 	 		out.println(intWarehouseCItemCount);
 	 		//Write the total value for warehouse C.
 	 		out.println(dblTotalValueC);
 	 		
 	 		//Close the output file.
 	 		out.close();	
 	 	 }
 	 	 catch (FileNotFoundException e)
		 {
			System.out.println("Error: Unable to open file for reading.");
		 }
		 catch (IOException e)
		 {	
			System.out.println("Error: Cannot read from file.");
		 }
 		 
 		 
 		 /**View the contents and the value of each warehouse.*/
 		 System.out.println("!---------------Inventory Summary------------------");
 		 System.out.println("!--------------------------------------------------");
 		 System.out.println("!--------------------LEGEND:-----------------------");
 		 System.out.println("!<item type>, <amount in stock>,<value of one item>");
 		 System.out.println("!--------------------------------------------------");
 		 System.out.println("!------------------Warehouse A:--------------------");
 		 System.out.println("!--------------------------------------------------");
 		 //Display Items in warehouse A.
 		 System.out.println(warehouseAList);
 		 
 		 //Total items in warehouse A.
 		 System.out.println("Total items: " + intWarehouseAItemCount);
 		 //Display total value of warehouse A.
 		 System.out.println("Total value: " + "$" + dblTotalValueA);
 		 
 		 System.out.println("!--------------------------------------------------");
 		 System.out.println("!------------------Warehouse B:--------------------");
 		 System.out.println("!--------------------------------------------------");
 		 //Display Items in warehouse B.
 		 System.out.println(warehouseBList);
 		 
 		 //Total items in warehouse B.
 		 System.out.println("Total items: " + intWarehouseBItemCount);
 		 //Display total value of warehouse B.
 		 System.out.println("Total value: " + "$" + dblTotalValueB);
 		
 		 System.out.println("!--------------------------------------------------");
 		 System.out.println("!------------------Warehouse C:--------------------");
 		 System.out.println("!--------------------------------------------------");
 		 //Display Items in warehouse C.
 		 System.out.println(warehouseCList);
 		 
 		 //Total items in warehouse C.
 		 System.out.println("Total items: " + intWarehouseCItemCount);
 		 //Display total value of warehouse C.
 		 System.out.println("Total value: " + "$" + dblTotalValueC);
 		 	
 	 }
 	
 	/**Display a help message.*/
 	private void displayHelp()
 	{
 		System.out.println("!--------------------------------");
 		System.out.println("! General Help:");
 		System.out.println("!--------------------------------");
 		System.out.println("! ");
 		System.out.println("!'help' display this help message.");
 		System.out.println("!'Q!' quit this program.");
 		
 		 		
 		System.out.println("!--------------------------------");
 		System.out.println("! Input File Specific Commands:");
 		System.out.println("!--------------------------------");
 		System.out.println("! ");
 		System.out.println("!'F' <name> type F followed by the name of the " +
 							"file to be used for input.");
 		
 		
 		System.out.println("!---------------------------------------");
 		System.out.println("! Input From Keyboard Specific Commands:");
 		System.out.println("!---------------------------------------");
 		System.out.println("! ");
 		System.out.println("!'K' <number> type K followed by the number of " +
 							 "items that will be added. ");
 							 
 		System.out.println("! ");
 	}
 }
Program file:
public class InventoryProg
 {
 	public static void main(String[] args)
 	{
 		//Create a new Inventory object.
 		Inventory test = new Inventory();
 		//Execute the command interpreter.
 		test.cmdInterpreter();
 	}
 }
Right now I am stuck on this and I cannot progress any further until I figure out how to input the data in the text file back into a arraylist.

Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 23 2007
Added on Apr 25 2007
2 comments
503 views