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?
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.
Re: Integer Too Large Error
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.
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 "";
}
}
Re: Integer Too Large Error
Your method 'milHours' says it's returning an int.
So why are you returning Strings?
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....
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...