| Author |
Methods and Arrays Help
|
Karen Wen
Greenhorn
Joined: Oct 24, 2010
Posts: 3
|
|
Hi, I've been stuck and completely lost on my Java class program since yesterday and I cannot seem to figure out how to resolve these issues since these are just the beginning parts of the program. Can someone guide/hint to me what I need? I will be putting up the parts where I'm stuck at and giving some general information.
I have two classes, SalesStats and Divisions.
In SalesStats class, I have a main that creates a Division object. I'm not sure what the arrays are suppose to be.
I am to read a file and store the data into two dimensional arrays with a method called readValues(Divisions d). This method is to store the file into array d.
The file input is like this:
1234
2345
3456
4567
12345
23456
34567
45678
etc...
I need to set this up so that the arrays will read as 6 rows (divisions) and 4 columns (quarters).
Output will be:
Division1:
1234
2345
3456
4567
Division2:
12345
23456
34567
45678
etc..
Here is the method I'm stuck at:
In Divisions class I'm having trouble with setting div and qrt.
Sorry if this is confusing, I will try to clear the problem more if there's a question.
Thank you!
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3036
|
|
Start with the setSales method:
What does final mean?
|
Steve
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32688
|
|
. . . and welcome to the Ranch
|
 |
Karen Wen
Greenhorn
Joined: Oct 24, 2010
Posts: 3
|
|
Hi and thank you! I have resolved my issues above, but now when I execute, I get a NullPointerException. Can someone guide me to what I need to do?
Codes in Divisions:
Codes in SalesStats:
|
 |
Gary Ba
Ranch Hand
Joined: Oct 23, 2009
Posts: 150
|
|
Did you allocate and assign anything for sales?
|
star chaser..
|
 |
Karen Wen
Greenhorn
Joined: Oct 24, 2010
Posts: 3
|
|
Gary Ba wrote:Did you allocate and assign anything for sales?
I don't think I did. Am I suppose to? How/where do I put it? I'm suppose to import a text file that stores the text into arrays as said on my first post.
Edit:
Ok, I've initially assigned sales. When executed, it's all 0.0 and not the numbers I need. Here are parts of the program. What am I doing wrong?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32688
|
|
|
You appear to be assigning amount to what is in sales for a particular square. Since you haven't done anything with sales yet, that will be 0. I suspect you want your assignment the other way round.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32688
|
|
If you have final ints, declare sales as new double[DIVISIONS][QUARTERS] rather than using 6 and 4. If you have the numbers, you might as well use them, and that style reduces the risk of errors like entering 5 by mistake.
Also, since you are assigning sales in the setSales method, you will create a new array every time you run that method and lose all you old information. Similarly for your getSales method. You want to create sales in a constructor, I think.
|
 |
 |
|
|
subject: Methods and Arrays Help
|
|
|