| Author |
How do I calculate the sum of a two dimensional arrays values?
|
matthew meltzer
Greenhorn
Joined: Sep 08, 2012
Posts: 24
|
|
Given the declaration: int[][] values = {{30},{10},{90}}
Write a class call Q18, and in this class write a method call doIt()
that takes the following variable "values" as declared above, as an
argument, and sums the values in the two dimensional array, to return
an int. Print this value to the standard output.
here is what I have so far:
It doesn't compile. I'm guessing it has to do with the for statement being in a method? I know my code is far off but my mind has been clogged from all these assignments and I can't seem to think straight.
Any help I can get so that I can figure out how to answer the question and fix the code would be greatly appreciated!
Thanks,
-Matthew Meltzer
|
 |
jatan bhavsar
Ranch Hand
Joined: Jul 23, 2008
Posts: 296
|
|
Hi Matthew,
doIt(values);
you need to have doIt method if you want use it. I cant see that method.
Refer this Algorithm and try to implement it.
Regards
Jatan
|
 |
matthew meltzer
Greenhorn
Joined: Sep 08, 2012
Posts: 24
|
|
jatan bhavsar wrote:Hi Matthew,
doIt(values);
you need to have doIt method if you want use it. I cant see that method.
Refer this Algorithm and try to implement it.
Regards
Jatan
Here is the code I have now.
This is the error I am still getting.
Main.java:8: incompatible types
found : int[]
required: int
for (int x : values)
|
 |
R. Jain
Ranch Hand
Joined: Aug 11, 2012
Posts: 276
|
|
matthew meltzer wrote:
Your values is an array of array. So, iterating over it will give you an array (not an int)
You should probably surround this for loop with one more loop. You will get the result..
|
OCPJP
|
 |
R. Jain
Ranch Hand
Joined: Aug 11, 2012
Posts: 276
|
|
matthew meltzer wrote:
Here is the code I have now.
And your above code will not give you what you desire, even though you make appropriate change..
You need to call the method doIt() from your main()..
Also, rather than initializing the array in your doIt() method, you should probably do it in main itself, and pass it as a parameter to the method doIt()
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4747
|
|
matthew meltzer wrote:Here is the code I have now...
And even if you didn't get an error, it would still be wrong.
Going back to the instructions you were given:
"Write a class call Q18, and in this class write a method call doIt() that takes the following variable "values" as declared above, as an argument, and sums the values in the two dimensional array, to return an int."
Does your doIt() method take an argument?Does it return an int?
What do you think you need to do to write it so that it does exactly what the instructions tell you?
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
matthew meltzer
Greenhorn
Joined: Sep 08, 2012
Posts: 24
|
|
|
I keep trying to follow the directions and change things but I can't get past the compilation errors. I have looked over my textbook multiple times and it does not mention multidimensional arrays used with for loops. I know this is easy stuff which i'm having trouble with, but I just can't for the life of me figure it out.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12912
|
|
Take it one step at a time.
First, look at Winston's last post. How would you declare a doIt() method that takes values as an argument, and that returns an int? Don't try to write any code for that method before you answer that question. Writing the code inside the method is step two.
Post here what you think the method declaration should look like. Then we'll proceed to help you write the code for it.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9946
|
|
matthew meltzer wrote:...but I can't get past the compilation errors.
We can't help you unless to post the compilation errors.
a piece of advice - never write more than 2-3 lines of code before you re-compile. If you write 100 lines then compile, you're bound to get dozens of errors. This can be very discouraging. if you write 2 lines at a time, then you know where to focus your attention. You should also test after each successful compilation. This may require to to write lots of dummy code that you quickly delete, but it actually saves time in the long run. I am constantly writing System.out.println() statements that print things like:
called method ABD
size of array is X
leaving method XYZ
looping again, index is <whatever>, max iterations is <whatever>
This lets me follow the flow of my code and lets me validate that my loop is really running the number of times I think it should.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
 |
|
|
subject: How do I calculate the sum of a two dimensional arrays values?
|
|
|