I have an assignment that I am working on, and this is my 3rd attempt at this course. The basic idea is that I have to store numbers in a 2D array. Then it has to be able to take these numbers and give the sum, average, etc... The numbers we are working with are tutoring hours and earnings. Finding the average earnings and total.
I have most of the assignment done, but I am having trouble accessing all the elements in the array. I found the "for each" loop. I think I have it coded properly, but it is throwing "wrong number type" errors. Netbeans is telling me that it I have myArray but need myArray.
I guess I am not understanding the syntax for a "for each" loop.
for(double element: myArray)
sum = sum + element
I don't understand how I am supposed to mix these number types. When i add --- for(double element: myArray)-- it throws errors in the body of the statement.
I am sorry, but I don't have my source code with me. I left it on the PC where I was working. I would like to work on it more tomorrow, so I was hoping I could get some helpful hints on for each loops.
You say you are working with 2-d arrays (technicality - you are not. Java only has 1-d arrays, but an array can have elements that are arrays...).
My GUESS is that myArray is what you think is your 2-d array, so when you iterate through it, you get back arrays. You need a nested loop to then iterate through the array you get. Something like:
Note: the above was written quickly, free-hand and may not be 100% syntactically correct.
I like to use an egg analogy. You want to store eggs. So, a 1-d array would be an egg carton. It can hold up to 12 eggs.
A 2-d array is like a crate of egg cartons. You can access any one carton in the crate, and then access an individual egg from the carton.
A 3-d array is like a pallate of crates of egg cartons.
you can keep going with this quite easily up to a fleet of trucks. You can access a specific truck in a fleet, then a specific palate in the truck, then a specific crate, then a specific carton, and then a specific egg.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
It is supposed to be a 2D array. I remembered that I emailed my source code to my instructor, so I downloaded it from my sent folder. I used "myArray" as a generic term for an array.
My actual array is more like this
This array is supposed to store current earnings and minutes tutored, entered through text fields. Each time you hit "Enter" it stores the info in the array. The minutes tutored in column 0, and the earnings in dollars and cents in column 1.
My "Enter" button code came out OK:
It is the "Run Report" button that I am having trouble with. When I hit "Run Report", the program is supposed to print out the entered info in rows, then print total earnings, average earnings, and where you rank regarding the current minimum wage. That is where the "for each" loop comes into play. I know I can do the algorithms for all of it, I just need to get the loop correct.
This is what I have so far
So, I am getting there. This last copy of my source was from before I started with the "for each" loop. When I get to the PC tomorrow, I will go over everything you have told me. Thank You!
Thank You Koen Aerts for that link, I can see that it will be very helpful