使用不同的.class文件找不到错误的方法

时间:2014-12-11 09:17:29

标签: java methods

我一直为printArray()获取NoSuchMethodError 我的教授要求我们使用他自己的.class文件来检查我们是否正确调用了我们的方法但是当我编译它时不使用他的.class文件我没有收到错误消息

public class ArrayOperations
{
    public static void main(String[] args) throws IOException
    {    

        System.out.print("Enter the data file name: ");
        Scanner keyboard = new Scanner(System.in);   
        String fileName = keyboard.nextLine();    

        int[] array = new int[1000];
        int index = 0;
        int indexCounter = 0;
        File file = new File(fileName);
        Scanner input = new Scanner(file);

        while (input.hasNext() && index < array.length)
        {
            array[index] = input.nextInt();
            index++;
            indexCounter++;
        }

        input.close();

        int[] finalArray = new int[indexCounter];

        for (int i = 0; i < indexCounter; i++){
            finalArray[i] = array[i];

        printArray(finalArray, n);
      }


    public static void printArray(int[] arrayList, int n)
    {

        for (int i = 0; i < arrayList.length; i++)
        {
            for (int value = 0; value < n; value++)
            {
                System.out.println(value + " ");
            }

            for (int value : arrayList){
                System.out.print(value);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您正在尝试在main方法中定义printArray方法,因此ocmpiler正在抱怨。

此外,您还没有声明要尝试调用方法的变量n。因此,请将调用用作:

printArray(finalArray, finalArray.length);
}//close main method

答案 1 :(得分:0)

位于printArray内的main method方法将其移到一边

public class ArrayOperations
{
public static void main(String[] args) throws IOException
{    

System.out.print("Enter the data file name: ");
Scanner keyboard = new Scanner(System.in);   
String fileName = keyboard.nextLine();    

int[] array = new int[1000];
int index = 0;
int indexCounter = 0;
File file = new File(fileName);
Scanner input = new Scanner(file);

while (input.hasNext() && index < array.length)
{
    array[index] = input.nextInt();
    index++;
    indexCounter++;
}

input.close();

int[] finalArray = new int[indexCounter];
for (int i = 0; i < indexCounter; i++)
{
    finalArray[i] = array[i];
}

printArray(finalArray, n);

}

//put it outside

public static void printArray(int[] arrayList, int n)
{
for (int i = 0; i < arrayList.length; i++)
for (int value = 0; value < n; value++) 
    System.out.println(value + " ");
for (int value : arrayList)
    System.out.print(value);
}
}
主要方法中未定义

Also n