Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

java - trying to call a class but it gives me errors: actual and formal argument lists differ in length

I created a class called PlacePiece which will be called in the Game class. but when I call it in Game Class it gives me the error:

actual and formal argument lists differ in length.

how do I fix it and what's causing it? I do not know how to fix it and been trying to solve it for eternity. I'm also new to coding so any feedback can help

public class Game {
    private Board gBoard;
    private PlacePiece placePiece; 

    public Game() {
        gBoard = new Board();
        PlacePiece.placePiece();
   }
}


import java.util.Scanner;

public class PlacePiece {

    public void placePiece(char[][] gBoard) {

        System.out.print("Game has begun, please enter where you would like to place (1~9):");
        Scanner scan = new Scanner(System.in);
        int placement = scan.nextInt();
  
        System.out.println(placement);       

        char piece = ' ';              
        switch(placement){
            case 1: gBoard[0][0] = piece;
                break;
            case 2: gBoard[0][2] = piece;
                break;
            case 3: gBoard[0][4] = piece;
                break;
            case 4: gBoard[2][0] = piece;
                break;
            case 5: gBoard[2][2] = piece;
                break;
            case 6: gBoard[2][4] = piece;
                break;
            case 7: gBoard[3][0] = piece;
                break;
            case 8: gBoard[3][2] = piece;
                break;
            case 9: gBoard[3][4] = piece;
                break;
        }  
        for(char[] row : gBoard){
            for(char c : row){
                System.out.print(c);
            }
            System.out.println();
        }
    }
}
question from:https://stackoverflow.com/questions/65926265/trying-to-call-a-class-but-it-gives-me-errors-actual-and-formal-argument-lists

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You haven't actually initialized any PlacePiece variable and your method isn't static, so how do you want to call it? Also in your placePiece function you should pass char[][] gBoard, that you aren't passing.

My guess is that you are trying to make a game and don't know how, sou you just copied some function from internet without knowing the function.

So, in your Board class I bet that you have something like this:

public class Board {
    private char[][] board; // you should initialize it in constructor

    public char[][] getBoard() {
        return board;
    }
}

Then you should create your placePiece variable in Game class and use it:

public class Game {
    private Board gBoard;
    private PlacePiece placePiece; 

    public Game() {
        gBoard = new Board();
        placePiece = new PlacePiece();
        placePiece.placePiece(gBoard.getBoard()); // you have to pass variable to placePiece() function
    }
}

I don't think you know, what you're doing otherwise you wouldn't have made these mistakes, so I highly recommend you taking some Java tutorial, so you have some foundation and then start doing more complex tasks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...