Help with file I/O and arrays

I am streaming a file into multiple arrays...

Page 1 of 1

3 Replies - 584 Views - Last Post: 18 February 2009 - 01:21 AM Rate Topic: -----

#1 ryanand88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 08-February 09

Help with file I/O and arrays

Posted 17 February 2009 - 08:31 PM

Here is my current code:


package assn4;
import java.io.*;
import java.util.*;
import java.text.NumberFormat;


/**
 *
 * @author Ryan
 */
public class assn4 {
   public static void main(String[] args){
	   int productCount=0;
	   Scanner input = new Scanner(System.in);
	   String regions[]={"North", "South", "East", "West"};
	   String regionRevenue[]=new String [3];
	   String products[]=new String [99];
	   int totalUnits[]=new int [99];
	   double totalRevenue[]=new double [99];
	   int northUnits[]=new int [99];
	   int southUnits[]=new int [99];
	   int eastUnits[]=new int [99];
	   int westUnits[]=new int [99];

	   //Allow user to enter path of file to be read
	   System.out.println("Enter the full path of the input file: ");
	   String filePath=input.next();

	   try{
			File inputFile=new File(filePath);

			Scanner file=new Scanner(inputFile);
 

			//Read values from file and fill arrays
			int i=0;
			while (file.hasNext()){
				products[i]=file.next();
				double price=file.nextDouble();
			  
				while (file.hasNextInt()){
					northUnits[i]=input.nextInt();
					southUnits[i]=input.nextInt();
					eastUnits[i]=input.nextInt();
					westUnits[i]=input.nextInt();
					totalUnits[i]=northUnits[i]+southUnits[i]+eastUnits[i]+westUnits[i];
					regionRevenue[0]+=northUnits[i]*price;
					regionRevenue[1]+=southUnits[i]*price;
					regionRevenue[2]+=eastUnits[i]*price;
					regionRevenue[3]+=westUnits[i]*price;
				}
				i+=1;
			}		 
				
	   file.close();
	   }
	   catch (Exception e){
		   System.out.println("Error reading file"+e.toString());
	   }



and here is a sample file of what I will be reading.

Computers	595.00	25	25	25	25
DiskDrives	325.50	100	200	300	250
Printers	99.95	100	100	100	100
WirelessCards	125.50	50	20	10	50
NetworkServers	1000.85	50	20	10	50



One of my issues is with my inner loop, I feel as if there must be another way to do this and save a few lines of code. The best alternative I can think of would be to avoid using 4 arrays for North, South, East, West.

Another issue is I do not want to declare my arrays with a length of 100 elements. This would leave a considerable amount of empty values. I wanted to have a counter variable to count the number of lines in the text file and use that variable for the number of elements in the array. I tried to do that within the outer loop, but I couldn't get it exactly right. The number of elements in these arrays would never exceed 100.

Any suggestions at all would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with file I/O and arrays

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: Help with file I/O and arrays

Posted 17 February 2009 - 08:33 PM

How about a vector? Then you can just push_back() a value as you stream it in.
Was This Post Helpful? 0
  • +
  • -

#3 ryanand88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 08-February 09

Re: Help with file I/O and arrays

Posted 17 February 2009 - 08:36 PM

Would you mind elaborating as I am unfamiliar with vectors, thanks.
Was This Post Helpful? 0
  • +
  • -

#4 ayman_mastermind  Icon User is offline

  • human.setType("geek");
  • member icon

Reputation: 126
  • View blog
  • Posts: 1,860
  • Joined: 12-December 08

Re: Help with file I/O and arrays

Posted 18 February 2009 - 01:21 AM

Quote

Would you mind elaborating as I am unfamiliar with vectors, thanks.

Check out these links to know more about vectors in Java:
http://leepoint.net/...ts/vectors.html
http://cda.morris.um...04/vectors.html
http://www.java2s.co...rinjavautil.htm
hope this helps, good luck ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1