Home  >  Article  >  java type mismatch: cannot convert from long to int

java type mismatch: cannot convert from long to int

WBOY
WBOYforward
2024-02-09 15:30:33528browse

In Java programming, type mismatch errors are a common problem. When we try to convert a long value to an int, we get the error "java type mismatch: cannot convert from long to int". This error message means that we are trying to convert a data type with a larger range to a data type with a smaller range, resulting in the risk of data loss or overflow. In this article, PHP editor Xinyi will analyze the cause of this error for you and provide solutions to solve it.

Question content

long A[]=new long[1000];
   Scanner a=new Scanner(System.in);
   for(long I=0;I<A.length;I++)
   {
    A[I]=a.nextLong();//error
   }
   return true;
}

This is why I'm getting a type mismatch: cannot convert from long to int

I don't understand why we get type mismatch because i is long and a[] is also long So why do we get a long to int type mismatch.

Solution

Use int instead of long in the for loop:

long A[]=new long[1000];
    Scanner a=new Scanner(System.in);
    for(int I=0;I<A.length;I++)
    {
        A[I]=a.nextLong();//error
    }

Unable to index long type array:https://www.php.cn/link/6dfa678a0fa26a0b36addfbc8fdc23e1

The above is the detailed content of java type mismatch: cannot convert from long to int. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete