Results 1 to 2 of 2
- 10-03-2011, 10:18 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 92
- Rep Power
- 0
function returning 0 for no reason.
Hey guys. Basically, i'm trying to convert from celsius to farenheit. Now, This function above is getting the correct format for input, and it isn't throwing an error message so I don't really understand where I'm going wrong with this. Basically, in the highlighted area, it just returns 0, without any mathematical justification. The ifstatement above it works fine though. Can you guys see anything wrong with it?Java Code:private int parseTemp(String inputTemp) { if((inputTemp.matches("[0-9]+c")) || (inputTemp.matches("[0-9]+C"))) { int newTemp = Integer.parseInt(inputTemp.substring(0,inputTemp.length() - 1)); return newTemp; } else if((inputTemp.matches("[0-9]+f")) || (inputTemp.matches("[0-9]+F"))) { // This section from here: int newTemp = Integer.parseInt(inputTemp.substring(0,inputTemp.length() - 1)); newTemp = (newTemp - 32) * (5/9); return newTemp; // : to here, returns 0 for some odd reason. } else { System.out.println("Sorry. Given string was not in the correct format."); return 0; } }
-
Re: function returning 0 for no reason.
You're doing int division: 5/9, which returns an int, here 0. You want to do double division instead: 5.0 / 9.0.
Similar Threads
-
Function call returning array of objects
By ShitalJain in forum New To JavaReplies: 4Last Post: 06-25-2011, 09:39 AM -
Best reason to not go to school ever
By Zack in forum Forum LobbyReplies: 5Last Post: 11-28-2010, 04:09 AM -
Need a reason for output
By Hemant16 in forum Threads and SynchronizationReplies: 3Last Post: 09-26-2010, 10:05 AM -
Getting errors for some reason
By Swarvy in forum New To JavaReplies: 7Last Post: 09-30-2008, 02:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks