java 不兼容的类型错误 int[] 到 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29786455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:56:57  来源:igfitidea点击:

Incompatible type error int[] to int

javaarraylistintjava.util.scanner

提问by Joseph Kraemer

I am getting an error, incompatible types: int[] cannot be converted to int. Can someone explain why this is happening and what I need to do to fix it? Thanks for the help. Here is my code:

我收到一个错误,类型不兼容:int[] 无法转换为 int。有人可以解释为什么会发生这种情况以及我需要做什么来解决它吗?谢谢您的帮助。这是我的代码:

public static String readFinalQuestionBank() throws Exception
    {   
        File textFile = new File("C:\Users\Joseph\Documents\School Files - NHCC\CSci 2002\FinalQuestionBank_JosephKraemer.txt");  //file location
        ArrayList<String> question;
        ArrayList<String> answer; 
        ArrayList<String> category;
        ArrayList<String> topic;


        int[] i = new int[0];
        String[] structure = {"OO Programming", "Generics", "Bags", "Complexity & Efficiency", "Stacks", "Recursion", "Sorts", "Queues",
                              "Lists", "Iterators", "Searching", "Associative Arrays/Hashing", "Trees/Heaps", "Graphs"};

        try
        {
            Scanner scan = new Scanner(textFile);                   //Scanner to import file

            while(scan.hasNextLine())                               //Iterator - while file has next line
            {
                String qBank = scan.nextLine();                     //Iterator next line
                String[] line = qBank.split("::");             //split data via double colon

                question[i]++ = line[0];    //error is here!!

                System.out.println(qBank);

            }
            scan.close();                  //close scanner

        }
        catch(FileNotFoundException e)
        {
             e.printStackTrace();
        }       
        return "";
    }//end method readFinalQuestionBank

回答by DDPWNAGE

int[]is an integer arraytype.

int[]是一种integer array类型。

intis an integertype.

int是一种integer类型。

You can't convert an array to a number.

您不能将数组转换为数字。

回答by Prudhvi

You have declared variable ias an intarray and using it to track index of question[]. Indices in arrays are represented by 0,1,2,3... which are of type regular intand not int[]. So you're getting the error int[] cannot be converted to int

您已将变量声明iint数组并使用它来跟踪question[]. 数组中的索引由 0,1,2,3... 表示,它们的类型为常规int而不是int[]。所以你得到了错误int[] cannot be converted to int

Solution:

解决方案:

Change int[] i = new int[0];to int i = 0;

更改int[] i = new int[0];int i = 0;

And also you have more problems in your code. You're not incrementing index value of question[]. So you're always copying the result from line[]array into 1st element of question[]which makes all the other elements in your array useless. Instead of using String array, use StringBuilderto save the value.

而且您的代码中还有更多问题。您没有增加 的索引值question[]。因此,您总是将line[]数组中的结果复制到第一个元素中,question[]这会使数组中的所有其他元素无用。不是使用String array,而是StringBuilder用于保存值。

回答by Sunil Rajashekar

You have multiple problems in the code. declare int as integer, initialise Questions and You have to convert String to integer before assigning it to question.

您在代码中有多个问题。将 int 声明为整数,初始化问题,您必须在将字符串分配给问题之前将其转换为整数。

int i = 0;
int [] question = new int [100];
question[i++] = Integer.parseInt(line[0]);