Results 1 to 8 of 8
Thread: Integer Too Large Error
- 03-04-2012, 11:08 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
Integer Too Large Error
Hey experts :)
I'm writing a program (my second on methods) that is supposed to convert military time to "normal" time. I think I have it figured out, expect that in my method, I have the following syntax. My problem is that I'm getting an "integer too large" error, but only for (hour == 08) and (hour == 09). Can someone explain why???Java Code:if (hour == 00) { return "12"; }
Also, just in case, here is my full code....if you see any other errors, I'd appreciate a heads up! :)
~Jaime
Java Code:public class TimeConvert { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter the time: "); int milTime = in.nextInt(); } /** A method to extract the minutes portion of an integer representing Military time. @param milTime, the military time provided: hhmm @return a string of the form 9:35 am or 7 pm */ public static String militaryToOrdinaryTime(int milTime) { //your work here int minutes = milTime % 100; int hour = milTime / 100; if (hour >= 12) { System.out.println(hour + ":" + minutes + "pm"); } else { System.out.pringln(hour + ":" + minutes + "am"); //convert hours if (hour == 00) { return "12"; } if (hour == 01) { return "1"; } if (hour == 02) { return "2"; } if (hour == 03) { return "3"; } if (hour == 04) { return "4"; } if (hour == 05) { return "5"; } if (hour == 06) { return "6"; } if (hour == 07) { return "7"; } if (hour == 08) { return "8"; } if (hour == 09) { return "9"; } if (hour == 10) { return "10"; } if (hour == 11) { return "11"; } if (hour == 12) { return "12"; } if (hour == 13) { return "1"; } if (hour == 14) { return "2"; } if (hour == 15) { return "3"; } if (hour == 16) { return "4"; } if (hour == 17) { return "5"; } if (hour == 18) { return "6"; } if (hour == 19) { return "7"; } if (hour == 20) { return "8"; } if (hour == 21) { return "9"; } if (hour == 22) { return "10"; } if (hour == 23) { return "11"; } return ""; } } }
- 03-04-2012, 11:12 PM #2
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
Re: Integer Too Large Error
I think I may have figured it out. I took off the "0" for each of the 1 digit hours and the error went away. Makes sense since a calculator won't show "08" it will only show "8". My new fun problem is that I"m getting errors on my "Scanner in = new Scanner(System.in)" line. Does it have something to do with where it's located? The "Scope" issue I just read about?
- 03-04-2012, 11:16 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
Re: Integer Too Large Error
Found the answer to this one too.....
Last one, I hope....I'm now getting this error: TimeConvert.java:57: error: missing return statement
Is it because I have return ""; ? And if so, what should it be? My book has that as the example for a similar exercise.
- 03-04-2012, 11:38 PM #4
Re: Integer Too Large Error
What's "pringln"?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-04-2012, 11:53 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
Re: Integer Too Large Error
Yah, typo. Should have been println.
Below is my most current code. I'm now getting errors on the quote marks in each of the "if" statements. I'm thinking it's because they may be for strings, and I'm trying to return an integer? However, I'm not sure how to fix it.
Java Code:import java.util.Scanner; public class TimeConvert { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter the time: "); int milTime = in.nextInt(); } /** A method to extract the minutes portion of an integer representing Military time. @param milTime, the military time provided: hhmm @return a string of the form 9:35 am or 7 pm */ public static String militaryToOrdinaryTime(int milTime) { //your work here String time = ""; int minutes = milTime % 100; int hour = milTime / 100; if (hour >= 12) { time = hour + ":" + minutes + "pm"; } else { time = hour + ":" + minutes + "am"; } return time; } /** Converts hours */ public static int milHours(int hour) { //convert hours if (hour == 0) { return "12"; } if (hour == 1) { return "1"; } if (hour == 2) { return "2"; } if (hour == 3) { return "3"; } if (hour == 4) { return "4"; } if (hour == 5) { return "5"; } if (hour == 6) { return "6"; } if (hour == 7) { return "7"; } if (hour == 8) { return "8"; } if (hour == 9) { return "9"; } if (hour == 10) { return "10"; } if (hour == 11) { return "11"; } if (hour == 12) { return "12"; } if (hour == 13) { return "1"; } if (hour == 14) { return "2"; } if (hour == 15) { return "3"; } if (hour == 16) { return "4"; } if (hour == 17) { return "5"; } if (hour == 18) { return "6"; } if (hour == 19) { return "7"; } if (hour == 20) { return "8"; } if (hour == 21) { return "9"; } if (hour == 22) { return "10"; } if (hour == 23) { return "11"; } return ""; } }
- 03-05-2012, 11:59 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Integer Too Large Error
Your method 'milHours' says it's returning an int.
So why are you returning Strings?Please do not ask for code as refusal often offends.
- 03-05-2012, 03:21 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
Re: Integer Too Large Error
I assumed I had to use strings because of the colon and the pm/am....when I made my TIME variable an int, I got a whole other set of errors....
- 03-05-2012, 03:28 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Integer Too Large Error
You have to return the type you have declared your method as returning.
So start there.
If you feel you need a String then change the return parameter to a String instead of an int, but what it should be is entirely down to your requirements.
Then again, you're not even using the method...Please do not ask for code as refusal often offends.
Similar Threads
-
Integer Comparison, Outputting Largest Integer Not Working
By killingthemonkey in forum New To JavaReplies: 4Last Post: 10-16-2011, 08:59 PM -
Error in unzip for large files
By magesh joghee in forum Advanced JavaReplies: 1Last Post: 08-03-2011, 04:35 PM -
Integer.parseInt() error
By niteangell21 in forum New To JavaReplies: 4Last Post: 02-06-2011, 05:36 AM -
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05-17-2010, 12:50 AM -
Integer.parseInt("5.843"); Error
By Cemi in forum New To JavaReplies: 3Last Post: 04-15-2010, 05:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks