Results 1 to 8 of 8
  1. #1
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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
    Java Code:
    if (hour == 00) { return "12"; }
    . My problem is that I'm getting an "integer too large" error, but only for (hour == 08) and (hour == 09). Can someone explain why???

    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 "";
       }
    }
    }

  2. #2
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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?

  3. #3
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,238
    Rep Power
    19

    Default Re: Integer Too Large Error

    What's "pringln"?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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 "";
       }
    }

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default 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.

    ** This space for rent **

  7. #7
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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....

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default 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.

    ** This space for rent **

Similar Threads

  1. Integer Comparison, Outputting Largest Integer Not Working
    By killingthemonkey in forum New To Java
    Replies: 4
    Last Post: 10-16-2011, 08:59 PM
  2. Error in unzip for large files
    By magesh joghee in forum Advanced Java
    Replies: 1
    Last Post: 08-03-2011, 04:35 PM
  3. Integer.parseInt() error
    By niteangell21 in forum New To Java
    Replies: 4
    Last Post: 02-06-2011, 05:36 AM
  4. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  5. Integer.parseInt("5.843"); Error
    By Cemi in forum New To Java
    Replies: 3
    Last Post: 04-15-2010, 05:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •