![]() |
|
[Java] Dividing an int by a double results in a double, How do I cast int? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Dividing an int by a double results in a double, How do I cast int? (/showthread.php?tid=5725333) |
Dividing an int by a double results in a double, How do I cast int? - WizardLizard - Sep 26, 2017 This is a project for class where I have to find the pace per mile needed to complete a certain distance at a certain time however it outputs something like 0:5:31.0 I'm having trouble casting the second's value to an int. Also, it's not required but how would I go about replacing the if else statements with a switch statement, couldn't really figure that out either because it's a string. Thanks. Code import javax.swing.JOptionPane;Note when trying to do double PaceSec = (int) TotalS / miles; it tells me that casting int is redundant and it doesn't change the output value at all. Also, I can't cast int to miles because it would change the time. * - UB3R5W4G0V3RL0RD - Sep 26, 2017 Regarding the casting: simply cast to an int in the popup menu. Regarding the switch: First you need to think about what we are looking at. You are comparing the "Distance" string with your input by using if-then-else statements. This means that "Distance" comes into our switch header. Code switch(Distance){}Now, what are we comparing it to? Spoiler (Click to View) These come into the body, or "switch block". A statement in the switch block can be labeled with one or more case or default labels. So, each case is one of your Strings to compare to: Code case "5K": //do stuff hereLastly, we must understand how the switch processes arguments. It runs from the top down and executes everything in its way that fits the bill. In your case this might not seem like an issue, but (surprise) it is. After the end of each case, include a "break". This jumps out of the switch, effectively doing 2 things for us: Making sure you don't keep checking the other cases, and saving time and processing power by not checking the other cases. Now you may be asking yourself, "But wait, I only have one possible case, don't I?". Don't forget that the default case will always be executed unless you exit the switch beforehand. * - MadZ - Sep 26, 2017 (Sep 26, 2017 10:41 AM)WizardLizard Wrote: Note when trying to do double PaceSec = (int) TotalS / miles; it tells me that casting int is redundant and it doesn't change the output value at all. you're casting 'TotalS' to an int; it is redundant because TotalS is already an int. you want (int) (TotalS / miles) or TotalS / (int) miles * - WizardLizard - Sep 26, 2017 (Sep 26, 2017 01:45 PM)UB3R5W4G0V3RL0RD Wrote: Regarding the casting: simply cast to an int in the popup menu. Alright I understand that but the reason I'm using if else statements is because I can ignore case, If I use a switch statement I can't really use that, or is there another implementation? Thanks for the explanation though it furthered my understanding of switch statements (Sep 26, 2017 05:42 PM)MadZ Wrote: you're casting 'TotalS' to an int; it is redundant because TotalS is already an int. Right but wouldn't that cause an issue regarding the times? for example half a marathon isnt exactly 13 miles but im wanting to cast int to my user output because the user wouldn't really care if its a decimal value or not they just want the times. * - razorfsh - Sep 27, 2017 You can clean up most of this code by removing what i would call the "idiot factor" replace: String Distance = JOptionPane.showInputDialog(null, "Enter your distance '5K', '10K', 'Half Marathon', or 'Full Marathon'."); with String Usroption = JOptionPane.showInputDialog(null, "Enter option numer 1-4 '1.) 5K', '2.) 10K', '3.) Half Marathon', or '4.) Full Marathon'."); //validate return here... /... int distancepicker =Integer.parseInt(Usroption); this returns a FIXED number that allows you to clear out all the shit relate to text and poor user input (which you don't validate any place), you can now use a case to select the actual distances, and clean out al that crap related to "Distance.equalsIgnoreCase("5K")){" etc * - UB3R5W4G0V3RL0RD - Oct 10, 2017 (Sep 26, 2017 06:46 PM)WizardLizard Wrote: Alright I understand that but the reason I'm using if else statements is because I can ignore case, If I use a switch statement I can't really use that, or is there another implementation? I don't really understand what you mean here, if you don't want to use the keyword "case" that's not going to work, because that's part of the switch statement (imagine leaving out the "if" from an "if" statement, it just belongs there). If you want to ignore cases, the switch only runs through the cases that are true, so if the second case returns "false", it won't do anything from it (i.e. case 2 is if your input is 2, then add 3 to it; if your input isn't 2, then it will just skip over this case and not add 3 to it). Using the "break" keyword will escape from the entire switch, so nothing after it will happen (Important: make sure it's in a case). This is like using "goto" or "jump" to escape a loop (like a "while" loop). You should obviously never actually escape a loop with goto, hence why it was removed in Java, but this is just for a better understanding. What the person above me said is also a really good idea, bonus points if you can properly implement radiobuttons into that. If I didn't answer your question properly, my apologies, see if you can maybe rephrase it and I'll try to help. |