Home > Java > body text

java type mismatch: cannot convert from long to int

WBOY
Release: 2024-02-09 15:30:33
forward
531 people have browsed it

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;
}
Copy after login

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
    }
Copy after login

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template