Home > Enterprise >  the literal x of type int is out of range
the literal x of type int is out of range

Time:10-19

The method is the following:

public static long repeatedString(String s, long n) {
    
    
    int convertedLong = (int) n;
    String repeated = new String(new char[convertedLong]).replace("\0", s);
    System.out.println(repeated);

    long numberOfAs = 0;
    for (int i = 0; i < convertedLong; i   )
    {
        if (repeated.charAt(i) == 'a')
        {
            numberOfAs  = 1;
        }
    }
    System.out.print(numberOfAs);
    return numberOfAs;
}

method works fine when the parameter n is small, however when it is large like 1000000000000 it throws an exception:

The literal 1000000000000 of type int is out of range

CodePudding user response:

The int type in Java can be used to represent any whole number from -2147483648 to 2147483647. It seems you've reach the limitation of what the int type can hold. Consider using long. As the method you are using takes a long.

From:

int convertedLong = (int) n;

To:

long convertedLong = n;

CodePudding user response:

All number literals in java, unless otherwise specified, are typed as an int. So what you'll need to do is define your literal as a long by adding the L suffix to the literal.

1000000000000L

Also why are you casting your long to an int? that seesm to defeat the purpose?

Check out this question for more info

CodePudding user response:

Issue is coming because your convertedLong is of type int. Why?

Make it long and also variable i to be long.

This error is coming because you are calling the function by just 1000000000.

Append L to it, to pass it as "long" like

100000000000L

else it will take as int.

CodePudding user response:

You are trying to call the method with the following parameters repeatedString("anyText",1000000000000) right? This would not even compile you have to extend a L at the end of your number to give the compiler the information that this is a long and not an int, so at first you call your function like this: repeatedString("anyText",1000000000000L) but if you think about it it's useless because you casting it to an int which then removes all the bytes which the int is not able to hold. So stop converting the long to an int and just use the given parameter as long.

CodePudding user response:

Try this.

static long repeatString(String s, long n) {
    int length = s.length();
    long mod = n % length;
    long count = 0, rest = 0;
    for (int i = 0; i < length;   i) {
        if (s.charAt(i) == 'a') {
              count;
            if (i < mod)
                  rest;
        }
    }
    return count * (n / length)   rest;
}

public static void main(String[] args) {
    String s = "aabca";
    for (long i = 0; i < 10;   i)
        System.out.println(i   " : "   repeatString(s, i));
    long x = 1000000000000L;
    System.out.println(x   " : "   repeatString(s, x));
}

output:

0 : 0
1 : 1
2 : 2
3 : 2
4 : 2
5 : 3
6 : 4
7 : 5
8 : 5
9 : 5
1000000000000 : 600000000000
  •  Tags:  
  • java
  • Related