3 Replies - 2324 Views - Last Post: 17 March 2010 - 05:24 PM Rate Topic: -----

#1 Murderality  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 40
  • Joined: 14-October 09

Storing Contents of Text File Into an Array

Posted 17 March 2010 - 02:59 PM

Hi there, I have a file called Quareterly Sales.txt the file looks exactly like so:

Quote

N
1
35.50
N
2
26.99
N
3
77.45
N
4
58.30
S
1
132.15
S
2
81.19
S
3
159.06
S
4
83.55
E
1
99.40
E
2
25.39
E
3
50.25
E
4
43.21
W
1
120.89
W
2
392.11
W
3
105.76
W
4
299.95
N
2
66.15
N
3
38.22
N
4
27.66
E
2
135.32
E
3
37.50
E
4
9.10


Essentially, the text document contains 4 divisions, N, S, E, and W (North, South, East, West). Along with the quarter period, and the sale amount. My program is going to read the text document, display the contents in a row and column fashion via a 2D array and also display the totals for each column and row much like a table of some sort. Anyways, I was just in the process of breaking up the devision, from the quarter, storing them in their own respective arrays, validating the arrays and then sticking them all in a 2D array for output. For no I'm just trying to store all of the Devisions for each quarter into an array. Based on the text file I believe there should be a total of 22 occurences of where N, W, S, or E appear. As you can see from the text document, the information is entered into a specific order. Starting with:
1. Division
2. Quarter
3. Sale ammount

in my program I read the first line, then skip three, read the line, and then skip another 3 and so on until the end of the file is reached. Here is where I perform this in my code:
public static void editSales() throws IOException
	{ //Start editSales method

		
	File file = new File("Quarterly Sales.txt");
	Scanner inputFile = new Scanner(file);	       
	FileReader fr = new FileReader(file);   
	LineNumberReader ln = new LineNumberReader(fr);
	int count = 0;
	
	do{ 	     
	count++;  
	} while (ln.readLine() != null);
	
	final int SIZE = count;
	String[] div = new String[SIZE];
	int index = 0; 
	int num = SIZE / 3;
	String[] devCodeArray = new String [num];
	   
	  
	
		while (inputFile.hasNext() && index < div.length){
			div[index] = inputFile.nextLine();
			index++;
		}
		inputFile.close();
		for(int i = 0; i < div.length; i += 3){
			//System.out.println(div[i]);
		devCodeArray[i] = div[i];			
		}

Now what I'm trying to do is store the first line and every third line after it into an array titled devCodeArray, however, it doesn't seem to be functioning properly as I get the following error:

Quote

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 24
at Quarterly_Sales_Report.editSales(Quarterly_Sales_Report.java:76)
at Quarterly_Sales_Report.main(Quarterly_Sales_Report.java:16)


I would appreciate if somebody told me how to fix this.

Also, if I were to print out all the lines that are read like so:
for(int i = 0; i < div.length; i += 3){
			System.out.println(div[i]);
		}

It displays all the division codes just like I wanted except it goes one over and displays a null at the end, why is this?

This post has been edited by Murderality: 17 March 2010 - 03:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Storing Contents of Text File Into an Array

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Storing Contents of Text File Into an Array

Posted 17 March 2010 - 03:11 PM

do{          
        count++;  
        } while (ln.readLine() != null


Your problem is here. The do...while will always iterate one more time than necessary, because it checks the condition after the loop iterates.

For example, if the file is completely empty, the loop will still run once.

Just use a standard while loop
Was This Post Helpful? 1
  • +
  • -

#3 Murderality  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 40
  • Joined: 14-October 09

Re: Storing Contents of Text File Into an Array

Posted 17 March 2010 - 05:16 PM

View Posterik.price, on 17 March 2010 - 02:11 PM, said:

do{          
        count++;  
        } while (ln.readLine() != null


Your problem is here. The do...while will always iterate one more time than necessary, because it checks the condition after the loop iterates.

For example, if the file is completely empty, the loop will still run once.

Just use a standard while loop


Oh thanks, that really helped out. But is there no way for me to store the first and every third line after it of a text file into an array?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10466
  • View blog
  • Posts: 38,793
  • Joined: 27-December 08

Re: Storing Contents of Text File Into an Array

Posted 17 March 2010 - 05:24 PM

Yes. Just read in the first line and assign it to an element in the array, read in the next 2 lines without assigning it, then read in the next line and assign again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1