Results 1 to 8 of 8
  1. #1
    SirJabalot is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default Can't see what is wrong

    Hi my first post here

    I was going to solve a problem and encountered this problem

    Can't tell why the long assignment is wrong

    Can anyone help me thanks

    Error says int is out of bounds


  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Error says int is out of bounds
    Try the following to see what the maximum value is that you use as an int constant:

    Java Code:
    System.out.println(Integer.MAX_VALUE);
    Edit:

    Artel shows how to use a long constant, which is the real answer you are looking for.
    Last edited by camickr; 05-04-2011 at 04:29 AM.

  3. #3
    Artel is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    This will compile without error:

    Java Code:
    import java.lang.Math;
    
    public class test {
      public static void main (String[] args) {
        long givenNum = 600851475143[COLOR="Red"]l[/COLOR];
        System.out.println(givenNum);
        for (int i = 1; i < Math.floor(givenNum/2); ++i) {
        }
      }
    }
    I'm kind of a beginner at Java too, so I probably don't know the real explanation, but I think when you assign "600851475143" to a long value, it treats 600851475143 as a literal int, so you have to put an "L" at the end of it so Java will treat it as a long literal instead of an int literal.
    Last edited by Artel; 05-04-2011 at 04:25 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I second Artel's recommendation and explanation but with one slight change: always use an upper case L not a lower case l at the end of your long literal as the lower case l is too easily mistaken for a one.

  5. #5
    SirJabalot is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you so much

  6. #6
    SirJabalot is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Ok this is pesky

    Even with the L gives an error saying out of int range


  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It works fine for me.

  8. #8
    SirJabalot is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. if anyone could help. what am i doing wrong?
    By glina126 in forum New To Java
    Replies: 12
    Last Post: 02-19-2011, 11:52 PM
  3. What is wrong?
    By phantom06 in forum New To Java
    Replies: 2
    Last Post: 02-02-2011, 06:08 PM
  4. can't see where it went wrong..
    By Yakg in forum New To Java
    Replies: 5
    Last Post: 01-31-2011, 12:19 PM
  5. Something is Wrong O.o
    By Spidermonkey in forum Advanced Java
    Replies: 16
    Last Post: 11-14-2010, 04:33 AM

Posting Permissions

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