Link to home
Start Free TrialLog in
Avatar of mk9000
mk9000

asked on

How do I read data from a text file and then put into several arrays?

I'm working on a program that keeps the inventory of a hardware store.
This program keeps the price, quantity, item id, and item name of each item in the store.  The data is then able to be accessed by the user who can print out an inventory report of everything, check if an item is in the store, and check the price of an item.

I got the program to work, but not in the way I'm supposed to.  I was wondering if someone could point me in the right direction for reading data from a text file and then enter it into several different arrays.  These arrays would be Name[] Quantity[] manPrice[] sellPrice[] pSold[] pOrdered[].  I got the program working by just entering the values into the program.  Now I need to try to make it work by reading from a text file instead.

Here's the code:
import java.io.*;  //Java input and output
import java.util.Arrays;  // Allows for arrays in the program
import java.text.NumberFormat;
 
 
public class hardwareInv   //Beginning of the class
 
{
	static  BufferedReader buf =
				 new BufferedReader(new InputStreamReader(System.in));
 
	public static void main(String [] args)  throws Exception 
 
		{       // Search chars on output
				String [] Name = new String[11];
       			String [] Id =  new String[11];
				int [] Price = new int[11];
				int [] Quantity = new int[11];
                int [] Ordered = new int[11];
                int [] Sold = new int[11];
                int [] ManPrice = new int[11];
 
				//Data for the arrays
	            Name[0] = "Plyers";
	            Name[1] = "Hammer";
	            Name[2] = "Tape";
	            Name[3] = "Screwdriver";
	            Name[4] = "Nails";
	            Name[5] = "Screws";
	            Name[6] = "Shovel";
	            Name[7] = "Broom";
				Name[8] = "Bolt";
			    Name[9] = "Washers";
			    Name[10] = "Tape Measure";
			    
 
				Id[0] = "1111";
				Id[1] = "2222";
				Id[2] = "3333";
				Id[3] = "4444";
				Id[4] = "5555";
				Id[5] = "6666";
				Id[6] = "7777";
				Id[7] = "8888";
				Id[8] = "9999";
				Id[9] = "0111";
				Id[10] = "0222";
				
 
				Price[0] = 600;
				Price[1] = 375;
				Price[2] = 855;
				Price[3] = 698;
				Price[4] = 565;
				Price[5] = 479;
				Price[6] = 1255;
				Price[7] = 659;
				Price[8] = 999;
				Price[9] = 479;
				Price[10] = 896;
 
 
                ManPrice[0] = 500;
				ManPrice[1] = 300;
				ManPrice[2] = 800;
				ManPrice[3] = 600;
				ManPrice[4] = 500;
				ManPrice[5] = 400;
				ManPrice[6] = 1200;
				ManPrice[7] = 600;
				ManPrice[8] = 900;
				ManPrice[9] = 400;
				ManPrice[10] = 800;
				
 
 
				Quantity[0] = 2;
	            Quantity[1] = 3;
	            Quantity[2] = 3;
	            Quantity[3] = 2;
	            Quantity[4] = 45;
	            Quantity[5] = 3;
	            Quantity[6] = 23;
	            Quantity[7] = 23;
				Quantity[8] = 25;
			    Quantity[9] = 12;
			    Quantity[10] = 34;
 
 
                Sold[0] = 34;
	            Sold[1] = 2;
	            Sold[2] = 76;
	            Sold[3] = 14;
	            Sold[4] = 31;
	            Sold[5] = 3;
	            Sold[6] = 5;
	            Sold[7] = 1;
				Sold[8] = 65;
			    Sold[9] = 18;
			    Sold[10] = 2;
 
 
                Ordered[0] = 2;
	            Ordered[1] = 4;
	            Ordered[2] = 1;
	            Ordered[3] = 6;
	            Ordered[4] = 8;
	            Ordered[5] = 9;
	            Ordered[6] = 3;
	            Ordered[7] = 4;
				Ordered[8] = 6;
			    Ordered[9] = 7;
			    Ordered[10] = 4;
			   
 
		//Getting Strings from the command line
 		String str = new String();
	    String name;
	    int number;
	    int counter=0;
 
	//Call Main Menu
 		do {
 
          str = MainMenu();
          number = Integer.parseInt(str);
 
          switch(number) //Beginning of switch
        	{
										
		  case 1:
			System.out.println("Check Prices");
		    counter = SearchMode(Name, Id, Price, Quantity);
		  break;
		  case 2:
            System.out.printf("%50s","MAX Hardware Store");
            System.out.println();
            System.out.println();
            System.out.printf("%6s%16s%14s%10s%8s%11s%12s","Item ID","Item Name",
                    "pOrdered","pInStore","pSold","manPrice","sellPrice");
				for (int i=0; i <= Name.length-1; i++)
				{
                 NumberFormat priceFormat = NumberFormat.getCurrencyInstance( );
                 System.out.println();
                 System.out.printf("%6s%18s%10s%9s%10s%12s%12s",Id[i],
                         Name[i],Ordered[i],Quantity[i],Sold[i],
                         priceFormat.format(ManPrice[i]),
                         priceFormat.format(Price[i]));
                 System.out.println();
                                                    
                }
                 int total=0;
                 int totalInv=0;
                 for (int i=0; i <= Name.length-1; i++)
                {
                                            
                 total+=(Price[i]*Quantity[i]);
                 totalInv+=Quantity[i];
                }
                 System.out.println();
                 NumberFormat priceFormat = NumberFormat.getCurrencyInstance( );
                 System.out.println( "Total Inventory: "
                 + priceFormat.format( total ) );
                 System.out.println("Total Items in Store: "+ totalInv);
                 break;
				 case 3:
				  break;
                 default:
				  break;
		   	    } //end of switch
                    }
        while (number !=3);
 
 
	 } // end of main
 
 
	 private static String MainMenu()
	 {
        String response =""; // Display for options and options for input
 
        System.out.println("\t\t\t*******************\n\t\t\tHardware Inventory"
                +"\n\t\t\t*******************\n\t\t\t");
	    System.out.println("\t\t\t(1) Check Prices");
	    System.out.println("\t\t\t(2) Display Inventory");
	    System.out.println("\t\t\t(3) Quit");
 
	    System.out.println("\t\t\tSelect (1-3): ");
 
		try {
  	    response = buf.readLine();
	    } catch (Exception e) {
	 			   System.out.println("\t\t\tBad Input");
	    }
  	    return response;
      }
	
 
 
 
private static int SearchMode(String[] Name, String[] Id,int[] Price,
            int[]Quantity) throws Exception
 
{				String str = new String();
 
			    int counter=0;
				int number;
 
	           	str = SearchMenu();
	        	number = Integer.parseInt(str);
 
 
    do {
 
 
 
 
	switch(number)
	    {
		case 1:
		 System.out.println("Search Menu - Name");
		 System.out.print("");
		 System.out.print("\t\t\tEnter The Product Name to Search: ");
		 String Name_Input = buf.readLine();
		 System.out.println("\t\t\tYour Entered: "+ Name_Input);
	     Arrays.sort(Name);
		 int Name_Search = Arrays.binarySearch(Name, Name_Input);
         System.out.println("\t\t\tItem Name: " +  Name[Name_Search]);
		 System.out.println("\t\t\tItem ID: " +  Id[Name_Search]);
		 System.out.println("\t\t\tPrice: " +  Price[Name_Search]);
		 System.out.println("\t\t\tQuantity: " +  Quantity[Name_Search]);
		break;
	    case 2:
		 System.out.println("Search Menu - Item ID");
		 System.out.print("");
		 System.out.print("\t\t\tEnter The Item ID to Search: ");
		 String ID_Input = buf.readLine();
		 System.out.println("\t\t\tYour Entered: "+ ID_Input);
                                                
		 Arrays.sort(Id);
		 int ID_search = Arrays.binarySearch(Id, ID_Input);
         System.out.println("\t\t\tItem Name: " +  Name[ID_search]);
		 System.out.println("\t\t\tItem ID: " +  Id[ID_search]);
	     System.out.println("\t\t\tPrice: " +  Price[ID_search]);
		 System.out.println("\t\t\tQuantity: " +  Quantity[ID_search]);
		break;
		case 3:
		 System.out.println("Search Menu - Prices");
		 System.out.print("");
		 System.out.print("\t\t\tEnter The Price to Search: ");
		 String Price_Input = buf.readLine();
		 System.out.println("\t\t\tYour Entered: "+ Price_Input);
   		 Arrays.sort(Price);
		 int Price_Search = Arrays.binarySearch(Price,
                 Integer.parseInt(Price_Input));
         System.out.println("\t\t\tItem Name: " +  Name[Price_Search]);
	     System.out.println("\t\t\tItem ID: " +  Id[Price_Search]);
	     System.out.println("\t\t\tPrice: " +  Price[Price_Search]);
		 System.out.println("\t\t\tQuantity: " +  Quantity[Price_Search]);
	    break;
		case 4:
 
                                            
	    //returns to main(String [] args);
		break;
	        	   
		default:
											     
		break;
	} //end of switch
        }
	    while (number !=4);
	    return 0;
 
}
 
 
 
 
private static String SearchMenu()
	 {
        String response ="";
 
        System.out.println("\t\t\t*******************\n\t\t\tSearch Menu\n\t\t\t"
               + "*******************\n\t\t\t");
	    System.out.println("\t\t\t(1) Search by Item Name");
	    System.out.println("\t\t\t(2) Search by Item ID");
	    System.out.println("\t\t\t(3) Search by Prices");
	    System.out.println("\t\t\t(4) Return to Main Menu");
 
 
	    System.out.println("\t\t\tSelect (1-4): ");
 
		try {
  	    response = buf.readLine();
	    } catch (Exception e) {
	 			   System.out.println("\t\t\tBad Input");
	    }
  	    return response;
      }
 
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mk9000
mk9000

ASKER

Thanks!