2

I am still new to Java, below given program was working absolutely fine until i changed all the int data types to long data types. Now it is giving me a loss of precision error and i don't understand why? I have changed all the data types to long, and yet i am getting this error. Please help.

public class Solution {

    public static void main(String[] args) {
        long i,j,great=0,gpos=0,k=1,temp=0;
        Scanner in = new Scanner(System.in);

        long n = in.nextLong();
        long[] scores = new long[n];

        for(long scores_i = 0; scores_i < n; scores_i++){
            scores[scores_i] = in.nextLong();
        }

        long m = in.nextLong();
        long[] alice = new long[m];

        for(long alice_i = 0; alice_i < m; alice_i++){
            alice[alice_i] = in.nextLong();
        }

        long rank[] = new long[n];

        for(j=0;j<n;j++){
            great = 0;
            for(i=0;i<n;i++){
                if(rank[i]!=0){
                    continue;
                }
                if(great<scores[i]){
                    great=scores[i];
                    gpos=i;
                }
            }

            if(temp==great){
                k--;
                rank[gpos]=k;
                k++;
            }
            else{
                rank[gpos]=k;
                temp=great;
                k++;
            }
        }

        for(i=0;i<m;i++){
            for(j=0;j<n;j++){
                if(alice[i]>scores[j]){
                    System.out.println(rank[j]);
                    break;
                }else if(alice[i]==scores[j]){
                    System.out.println(rank[j]);
                    break;
                }else if(j==(n-1)){
                    System.out.println(rank[j]+1);
                    break;
                }
            }

        }

        in.close();
    }
}

Here is the error report that i keep getting:

Solution.java:11: error: possible loss of precision
        long[] scores = new long[n];
                                 ^
  required: int
  found:    long
Solution.java:14: error: possible loss of precision
            scores[scores_i] = in.nextLong();
                   ^
  required: int
  found:    long
Solution.java:18: error: possible loss of precision
        long[] alice = new long[m];
                                ^
  required: int
  found:    long
Solution.java:21: error: possible loss of precision
            alice[alice_i] = in.nextLong();
                  ^
  required: int
  found:    long
Solution.java:24: error: possible loss of precision
        long rank[] = new long[n];
                               ^
  required: int
  found:    long
Solution.java:29: error: possible loss of precision
                                if(rank[i]!=0){
                                        ^
  required: int
  found:    long
Solution.java:32: error: possible loss of precision
                                if(great<scores[i]){
                                                ^
  required: int
  found:    long
Solution.java:33: error: possible loss of precision
                                        great=scores[i];
                                                     ^
  required: int
  found:    long
Solution.java:40: error: possible loss of precision
                                rank[gpos]=k;
                                     ^
  required: int
  found:    long
Solution.java:44: error: possible loss of precision
                                rank[gpos]=k;
                                     ^
  required: int
  found:    long
Solution.java:52: error: possible loss of precision
                                if(alice[i]>scores[j]){
                                         ^
  required: int
  found:    long
Solution.java:52: error: possible loss of precision
                                if(alice[i]>scores[j]){
                                                   ^
  required: int
  found:    long
Solution.java:53: error: possible loss of precision
                                        System.out.println(rank[j]);
                                                                ^
  required: int
  found:    long
Solution.java:55: error: possible loss of precision
                                }else if(alice[i]==scores[j]){
                                               ^
  required: int
  found:    long
Solution.java:55: error: possible loss of precision
                                }else if(alice[i]==scores[j]){
                                                          ^
  required: int
  found:    long
Solution.java:56: error: possible loss of precision
                                        System.out.println(rank[j]);
                                                                ^
  required: int
  found:    long
Solution.java:59: error: possible loss of precision
                                        System.out.println(rank[j]+1);
                                                                ^
  required: int
  found:    long
17 errors
  • @JohnColeman Arrays expect an int. Giving them a long is too much data, you need to cast it as an int for the compiler to be happy. The possible loss of precision comes from discarding the top half of the long – phflack Dec 24 '17 at 13:25
  • https://stackoverflow.com/questions/14571557/create-an-array-of-long – Ousmane D. Dec 24 '17 at 13:26
  • https://stackoverflow.com/questions/39586612/confused-of-the-error-when-creating-a-long-array-using-a-long-variable-as-its-si?noredirect=1&lq=1 – Ousmane D. Dec 24 '17 at 13:27

1 Answers1

2

You cannot use a long as an array index, and the maximum possible length of an array is Integer.MAX_VALUE.

Therefore any variable used as an index of an array should be int:

for(int alice_i = 0; alice_i < m; alice_i++){
    alice[alice_i] = in.nextLong();
}

The same goes for all the variables you use as array indices.

See JLS 10.4. Array Access:

Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion (§5.6.1) and become int values.

An attempt to access an array component with a long index value results in a compile-time error.

Community
  • 1
  • 1
Eran
  • 387,369
  • 54
  • 702
  • 768