简体   繁体   中英

Incompatible types when creating new string array and cannot find symbol when accessing another class variable

Incompatible types when creating new string array and cannot find symbol when accessing another class variable.

I have two classes:

  • Sistem
  • MataKuliah

Sistem class:

/*
    Test array

*/
import java.util.Scanner;

class Sistem
{
        public static void main (String args[]){
            int counter = 0, jumlahMk;
            String[] namaMk = new String[jumlahMk];


            Scanner in = new Scanner(System.in);
            MataKuliah mk = new MataKuliah();

            jumlahMk = in.nextInt();
            in.nextLine();
            while(counter<jumlahMk){
                namaMk[counter] = (new String[jumlahMk]);
                namaMk[counter] = in.nextLine();
                counter++;
            }

            mk.printNamaMatkul(namaMK);


        }//end main
}//end class

MataKuliah class:

/*
    MataKuliah class
*/

class MataKuliah{
    void printNamaMatkul(String[] namaMk){
        System.out.println(Sistem.namaMk);

    }
}

I know it's basic, please help me! :)

You can only access fields in another class. A better approach is to pass the data you want to use, which is what you actually did.

void printNamaMatkul(String[] words){
    System.out.println(Arrays.toString(words));
}

The name of the parameter doesn't have to match the argument you passed to it. ie you can write

printNamaMatkul(namaMk);

but it would make more sense to make the name of the method generic

void printArray(String[] words){
    System.out.println(Arrays.toString(words));
}

printArray(namaMk);

There are a couple of issues with your program.

  1. namaMk[counter] = (new String[jumlahMk]); This statement is incorrect as you are trying to convert String to String[] here.
  2. You have not given the size of namaMk Array.
  3. You are trying to refer the private member of another class by doing System.out.println(Sistem.namaMk);
  4. You are creating the object of class MataKuliah directly in static main block.

You need to correct all the above problem to get your code running.

Here is the corrected code snippet:

public class MataKuliah {
    public void printNamaMatkul(String[] namaMK) {
        System.out.println(Arrays.toString(namaMK));
    }
}

class Sistem
{
    public static void main (String args[]) {
        /* Create a new instance of Sistem */
        Sistem sistem = new Sistem();
        sistem.run();
    }

    private void run() {
        int counter = 0, jumlahMk;

        Scanner in = new Scanner(System.in);
        /* This will be valid now */
        MataKuliah mk = new MataKuliah();

        jumlahMk = in.nextInt();
        /* Create the String[] Array */
        String[] namaMk = new String[jumlahMk];
        in.nextLine();
        while(counter < jumlahMk){
            namaMk[counter] = in.nextLine();
            counter++;
        }
        mk.printNamaMatkul(namaMk);
    }
}

Input:

3
foo
bar
foobar

Output:

[foo, bar, foobar]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM