Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!

Michael Payte

Greenhorn
+ Follow
since Sep 14, 2012
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Michael Payte

Campbell Ritchie wrote:By the way, if that program is an applet rather than a frame. shouldn’t the code go in the init and paint methods rather than the constructor? I haven’t written applets for ages, so I can’t remember, but that doesn’t look right to me.

Actually, I will be changing the JApplet to JFrame. I just haven't done that yet.
5 years ago
I'm trying to make it so that my application will display a message by right-clicking a button (the message must be displayed where the right-click event is detected). However, when I right-click on the buttons, nothing happens. Help would be appreciated; thank you in advance! Here is my complete code (130-163 contains the mouse listener code. mousePressed, mouseEntered, mouseReleased, and mouseExited are intentionally left empty):

5 years ago
Actually, I don't want the button to cycle through all of the colors with a single click; I want the buttons to change to the next color with a single click. I guess I wasn't too clear about that, sorry. I want the first click to change the button's color to blue, the second to change it to red, the third to yellow, the fourth to magenta, the fifth to pink, the sixth back to green, the seventh to blue again, etc.
5 years ago
I have two colored buttons. For each button, pressing it is supposed to change its color. They are supposed to cycle through six different colors, with the sixth button press cycling back to the original color. However, when I click either button, they don't change color. Please help, thank you!

EDIT: To clarify, I don't want the button to cycle through all of the colors with a single click; I want the buttons to change to the next color with a single click. I want the first click to change the button's color to blue, the second to change it to red, the third to yellow, the fourth to magenta, the fifth to pink, the sixth back to green, the seventh to blue again, etc.

5 years ago

fred rosenberger wrote:You might want to consider trimming that down. I doubt many people are going to try and dig through over 400 lines of code to solve your problem. If you could strip out any irrelevant parts, but leave a working program that we can compile and run, you are MUCH more likely to get the assistance you are requesting.

Just trimmed it down. It's half as long now.
5 years ago
In this program, the user is required to input employee names (Strings) and payment info(integers), and it's all put into a 2-dimensional array as a table. I'm trying to get the program to write the array info to a .txt file. However, when I open the .txt file, everything is all messed up, with a bunch of integers replaced with "0" and Strings replaced with "null". For example:

dan,
23,5,1,2,3,4,5null,
0,0,0,0,0,0,0null,
0,0,0,0,0,0,0

the info should actually be like this:

dan,23,5,1,2,3,4,5
terry,34,7,2,5,3,5,8
joe,45,6,4,2,9,3,6

The program's full code is below. Any >< is supposed to be < (I don't know why it does that when I put the code into the forums). The part that's supposed to write the info to a .txt file is lines 130-157. Help would be appreciated, thanks!


5 years ago

Rajdeep Biswas wrote:Hi,
The following idea might help.

Edit: Ahh!! yes, try to make a more generic method and build the validation logic inside that, and enjoy OOP modularity...
Regards,


What's br? It's not defined anywhere.
5 years ago
Okay, so for this program I'm trying to make, user input is required for several integer values. I've made it so that if the user enters a value other than an integer, the program will output that the user input must be an integer, and prompt for input again. The problem is, though, the user input here is then applied to the next thing that requires input, completely skipping what the user had given improper input in the first place. For example, if the user inputted a String when prompted for hours worked on Monday, and they enter "8" when prompted again after the error message appears, that 8 will not be applied to hours worked on Monday, but rather the next thing the program asks for input for: hours worked on Tuesday. The program just assigns a zero to hours worked on Monday. I would really appreciate help in fixing this problem. Thanks!

My program(the problem area is 125-199. Anything that appears as >< is just supposed to be <. I'm not sure why it shows up like that when I paste the code into the forums):
5 years ago

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

5 years ago

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.
5 years ago

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:

5 years ago

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.
5 years ago
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.

5 years ago