0

Quite new to Java so apologies if there's any wrong terminology or some simple mistakes here. I'm trying to create an object of a class (Player) in another class (Main), by calling a constructor method in Main (I declared the constructor in Player). I'm getting the 'cannot find symbol' error, which to me means that I've declared the variables outside of the scope of the constructor, but I really don't know. Here is the code:

Player:

    public class Player { 

  public Player(String Name, int MA, int ST, String AG, String PA, String AV, int Cost, String Skills) {
    Name = "BLANK";
    MA = 0;
    ST = 0;
    AG = "1+";
    PA = "1+";
    AV = "1+";
    Cost = 0;
    Skills = "None";

  }     //Declares all class attributes as parameters for when a new object is created. Target numbers and skills are strings, simple numbers are integers

Main:

import java.util.Scanner;


//goals: select from teams, store player data in external files, allow roster editing, show recommended rosters


class Main {
  public static void main(String[] args) {

    Scanner Input = new Scanner(System.in); //scanner object called Input to allow input

    System.out.println("BB2020 TEAM BUILDER");
    
    Player NewPlayer = new Player("Human Lineman", 6, 3, "3+", "4+", "9+", 50, "None");
    System.out.println(NewPlayer.Name);
    
    
  }
}

Again sorry if this is a common or simple question, thanks for your help :)

  • Hi Matthew. Do you have any packages? How are you compiling your source? – KeyMaker00 Apr 14 '21 at 10:48
  • Hi Matthew, Can you also add the paths of the files in which these classes are written? – MD.Tabish Mahfuz Apr 14 '21 at 10:54
  • @SergeyAfinogenov same issue is still happening after making this change – Matt Jones Apr 14 '21 at 11:02
  • I'm using Repl.it's Java compiler, and i've imported the Scanner class. I can't provide the file path as repl won't allow this @KeyMaker00 @ MD.Tabish Mahfuz – Matt Jones Apr 14 '21 at 11:03
  • If Player.java file location is not the same as Main.java, then use import statement in Main class to get acces to Player. Use this.name in construnctor and create public fields in Player class or use setters and getters methods. Make Main public. Use lowercase for variables. @Matt Jones – Sergey Afinogenov Apr 14 '21 at 11:15

3 Answers3

0

This line is your issue System.out.println(NewPlayer.Name);

The reason it gives you the error is that there is no value/field in the Player class called Name. To fix this you need to create variables inside the class (not the method). For example, this will do the trick:

public class Player { 
    //Create empty class variables
    String Name;
    int MA;
    int ST;
    String AG;
    String PA;
    String AV;
    int Cost;
    String Skills;

    public Player(String Name, int MA, int ST, String AG, String PA, String AV, int Cost, String Skills) {
        //Assign the values passed into this method to the class variables using the "this" keyword
        this.Name = Name;
        this.MA = MA ;
        this.ST = ST ;
        this.AG = AG ;
        this.PA = PA ;
        this.AV = AV ;
        this.Cost = Cost ;
        this.Skills = Skills ;
    }
}

And now the correct output will be printed:

Human Lineman

Caution: In Java there is a universal code convention to never name variables with a capital letter at the start (Instead of String Name; use String name;), this is done to make it clear what is a variable and what is a class (Only classes should have a capital letter at the start). It helps to troubleshoot issues as well as giving clean consistent code. Give it a go in your projects going forward.

sorifiend
  • 5,927
  • 1
  • 28
  • 45
0

First of all, as said above me, you need to define the variables first. Second, you could either use "System.out.println(NewPlayer.Name);" if you define the variable to 'public' - for example "public String name", Another way to do it is to define the variable to be 'private' - "private String name", and use a variable.get() method in your 'Player' class

Levi
  • 1
  • Note: The variables do not need to be declared public (they will be accessible via the class object by default) unless they are also static (which is not what OP is trying to do). – sorifiend Apr 14 '21 at 11:27
0
  1. Use this."fieldName" in constructor and create such fields in Player class. You can use getter methods to get this field in other class.
  2. If Player.java file location is not the same as Main.java, then use import statement in Main class to get access to Player.
  3. Make Main public.

P.S. Use lowercase for variable first letter

Sergey Afinogenov
  • 2,137
  • 4
  • 13