GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Adding products of numbers in an array, and string output problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding products of numbers in an array, and string output problem" Watch "Adding products of numbers in an array, and string output problem" New topic
Author

Adding products of numbers in an array, and string output problem

Michael Payte
Greenhorn

Joined: Sep 14, 2012
Posts: 14
In this code, I have a table with three employees, their names, ages, their total earnings per day for Monday through Friday(which the program calculates by multiplying payment per hour with hours worked each day, both of which are provided by user input), and their total earnings for the week. The program uses arrays to store this data. My main problem is that I can't get the program to properly add up the daily earnings and obtain/display the final weekly earnings. Right now, I've purposefully only implemented the bit of code that's supposed to (but doesn't actually) add them to only apply to the first row of the table, just so you know. When I attempt to implement this bit of code, it outputs the daily earnings with numbers in front of them (for example, if the daily earnings are 5, 10, 15, 20, 25, the program outputs them as 5, 110, 315, 620, & 1025, and displays a 15 where the week total is supposed to be). I really can't figure out how to fix this.

Also, the program is supposed to prompt for string inputs a total of three times (for the employee's names), but it only prompts it once, and uses the name from that one prompt for all three names on the table. I can't figure out how to fix this, either.

Simply put, I'm stuck, and I could really use some help. Thank you very much!

By the way, wherever it says ><, it's supposed to be <. Not sure why this post is going that; it's not part of my code.

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

For your first issue, where the program skips the line while reading the input, you should add input.nextLine() after last input.nextInt() for each employee. The problem is that input.nextInt() reads only the integer value, and \n character (as a result from pressing return) is picked up by the following input.nextLine(). So, something like this:


Next, why you use for loop here, when you only access single element from array:


Finally, the main issue here is a result of a wrong logic in your for loop (lines 196-209 of your code). In this case info[i][j] contains number of hours worked per each day, not the payment per day. But you're still trying to add it to your total so what you're actually doing is calculating total number of hours worked per week. Also, you're printing totalScore in your inner for loop, which results in getting incorrect output; that should be moved after the end of the inner for loop.


The quieter you are, the more you are able to hear.
Michael Payte
Greenhorn

Joined: Sep 14, 2012
Posts: 14
Kemal Sokolovic wrote:For your first issue, where the program skips the line while reading the input, you should add input.nextLine() after last input.nextInt() for each employee. The problem is that input.nextInt() reads only the integer value, and \n character (as a result from pressing return) is picked up by the following input.nextLine(). So, something like this:


Next, why you use for loop here, when you only access single element from array:


Finally, the main issue here is a result of a wrong logic in your for loop (lines 196-209 of your code). In this case info[i][j] contains number of hours worked per each day, not the payment per day. But you're still trying to add it to your total so what you're actually doing is calculating total number of hours worked per week. Also, you're printing totalScore in your inner for loop, which results in getting incorrect output; that should be moved after the end of the inner for loop.

Thank you, you've been very helpful! My program now prompts for all three employee names, and it calculates & displays all of the daily earnings properly. Unfortunately, though, the table in the program's output still uses the first employee's name for all three employees, and the weekly totals are now being added to each other (for example, let's say that the weekly totals are 15, 20, and 25. The program displays them as 15, 35, and 60). Additional help would be appreciated!

EDIT: I've fixed the problem of the accumulating weekly totals.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

In that for loop we were discussing, you are actually always accessing empoyee with index 0 (the first employee). I thought you put that just for testing purposes, since there were some commented parts.
This for loop:

should be the right one.

If you followed all directions, the program should give this output:

As I understood, this is the output you're expecting.

Note that I commented those two for loops following the one we're discussing about, because I don't really see their point (they are supposed to do some calculations, but implementing the same wrong logic). If they are supposed to actually do something, you'll need to provide more details.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Few more tips for you.
You should definitely change the design of your class(es). One solution would be to implement Employee class where you would have private fields, such as: name, age, hourlyPayment and an array of 5 values (each value representing hours worked per day). Those fields should not be left without access modifier (like you did) since default access modifier is a tricky thing; all the classes in the same package can access them, and that's not consistent with OO methodology. Instead, declare them as private and provide appropriate getter/setter for them.

Furthermore, you should implement a method (e.g. calculateWeeklyEarning()) that does the job you are doing in main method. Simply, it would iterate over an array to get the sum of hours per per week, multiply it by hourlyPayment and return the total payment per week. Also, overriding toString() method would be a good call, since you would then just invoke it when you want to print details of your employee. That way your Employee class would encapsulate all the logic for employees inside that class (which is the correct design), and your main method would certainly be shorter than the one you have.
Michael Payte
Greenhorn

Joined: Sep 14, 2012
Posts: 14
Kemal Sokolovic wrote:In that for loop we were discussing, you are actually always accessing empoyee with index 0 (the first employee). I thought you put that just for testing purposes, since there were some commented parts.
This for loop:

should be the right one.

If you followed all directions, the program should give this output:

As I understood, this is the output you're expecting.

Note that I commented those two for loops following the one we're discussing about, because I don't really see their point (they are supposed to do some calculations, but implementing the same wrong logic). If they are supposed to actually do something, you'll need to provide more details.

There must be something I missed, because the program terminates with an array index out of bounds exception, specifying line 211. The following is my full current code(again, any >< are supposed to be <):


This is an example of my input and the output I get:

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

You commented the line 197 where you were initializing array you were using in your original code. You are now initializing it at line 201 as 1-length array, but at line 211 that throws the exception you are accessing totalScore[i] where i has the value of 2 in second iteration. Either uncomment and use previous declaration, or declare totalScore as int variable, not an array, and change the rest of the code that used an array to use just variable.
Michael Payte
Greenhorn

Joined: Sep 14, 2012
Posts: 14
Kemal Sokolovic wrote:You commented the line 197 where you were initializing array you were using in your original code. You are now initializing it at line 201 as 1-length array, but at line 211 that throws the exception you are accessing totalScore[i] where i has the value of 2 in second iteration. Either uncomment and use previous declaration, or declare totalScore as int variable, not an array, and change the rest of the code that used an array to use just variable.
Excellent, it all works just fine now! There's just a bit more left to the program that I hadn't mentioned earlier because I wasn't focused on it at the time. The program needs to calculate and display the minimum and maximum amounts per day and for the whole week, along with the corresponding employee's names. For example: "On Monday, Bill had the maximum amount with 50, and John had the minimum amount with 15. On Tuesday, Carol had the maximum amount with 35, and Bill had the minimum amount with 12... For the whole week, John had the maximum amount with 150, and Carol had the minimum amount with 120." That should be all that's left to finish.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Glad it's all ok now.
For the other part, you'll have to come up with some solution, and if it's not working come back here to resolve it. Nobody here will implement that for you.

edit: By the way, welcome to the Ranch!
Michael Payte
Greenhorn

Joined: Sep 14, 2012
Posts: 14
Kemal Sokolovic wrote:Glad it's all ok now.
For the other part, you'll have to come up with some solution, and if it's not working come back here to resolve it. Nobody here will implement that for you.

edit: By the way, welcome to the Ranch!

Thanks for the welcome! Well, I've gotten things mostly figured out, except for one detail: I can't get the names to properly correspond to the minimum & maximum values. For example, let's say that "Jake", the first employee that data was inputted for, has the highest total earnings for the week at 132. Since I used Array.sort, my array totalScore now has the 132 at element [2] (the highest element, since totalScore has 3 elements total), where it was element [0] before the sorting. However, let's say that "Chris" was the third employee that data was inputted for (and therefore the one with data originally assigned to element [2] before the sorting) with the lowest total earnings of 110 (therefore at element [0] after the sorting). The problem is that the program's output would read, "For the whole week, Chris earned the maximum dollar amount with $132, and Jake earned the minimum dollar amount with $110." The names would turn out wrong. I really don't know how to get the names to correspond properly. As always, help would be very much appreciated! Here's my current code(the code for all the sorting is near the bottom):

 
GeeCON Prague 2014
 
subject: Adding products of numbers in an array, and string output problem