How to fix this instance of "can't find symbol"?

I have been given a driver which looks like the following:

public class LabTest_Driver
{
    public static void main (String[] args)
    {
        int[] list = new int[20];
        System.out.println();
        makeList (list);
        System.out.print("\n" + "The original (unsorted) list is:" + "\n");
        showArray(list);
                .
                .
                .
    }
}

The makeList constructor is suppose to make an array of the length of list with 20 different non repeating numbers between 100 and 199(inclusive)

This is my class so far:

import java.util.Scanner;
import java.util.Random;

public class LabTest
{
     private static int[] list;

public void makeList(int[] list)
{
    Random ran = new Random();
    this.list = list;
    int n = list.length;
    for (int element : list)
    {
        int w = ran.nextInt(100) + 99;
        list[element] = w;
        w = 0;
    }
}

public void showArray(int[] b)
{
    for (int element : list)
    {
        System.out.print(list[element] + " ");
    }
}

}

When I press compile it comes up with an error message saying "cannot find symbol - method makeList(int[])", but didn't I make that constructor correctly. If anyone could clarify this and help me with my programming problem that would be amazing.

Upvotes: 1

Views: 758

Answers (1)

ninja.coder
ninja.coder

Reputation: 9648

It is because you have two different classes for Lab_Driver and LabTest. You have to create a new Object of class LabTest and then invoke your methods i.e. makeList() and showList() on that Object. Alternatively, if the two methods are static you can call them directly by prefixing them with LabTest.

Here is the corrected code snippet:

public static void main (String[] args)
{
    int[] list = new int[20];
    System.out.println();
    LabTest labTest = new LabTest();
    labTest.makeList(list);
    System.out.print("\n" + "The original (unsorted) list is:" + "\n");
    labTest.showArray(list);
            .
            .
            .
}

Alternatively, you can do this if you make your methods static:

public static void main (String[] args)
{
    int[] list = new int[20];
    System.out.println();
    LabTest.makeList(list);
    System.out.print("\n" + "The original (unsorted) list is:" + "\n");
    LabTest.showArray(list);
            .
            .
            .
}

Upvotes: 1

Related Questions