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
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)
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

New Topic/Question
Reply




MultiQuote








|