zawl
zawl

Reputation: 47

Where to add main method when it says class does not have a static void main method accepting String[]

I'm trying to make my program named ZawTennisPlayer to output the following using constructors and display methods. Desired sample output I am using a sample code called TestTennisPlayer2 to test it out and to get the desired output. My current code is:

public class ZawTennisPlayer

{
//instance variables
private String playerName;
private String country;
private int rank;
private int age;
private int wins;
private int losses;

//default constructor
public ZawTennisPlayer()
{
playerName=null;
country=null;
rank=0;
age=0;
wins=0;
losses=0;

}

//parameterized constructor
public ZawTennisPlayer(String playerName,String country)
{
this.playerName=playerName;
this.country=country;
rank=0;
age=0;
wins=0;
losses=0;

}

//parameterized constructor
public ZawTennisPlayer(String playerName,String country,int rank, int age)
{
this.playerName=playerName;
this.country=country;
this.rank=rank;
this.age=age;
wins=0;
losses=0;
}

//parameterized constructor
public ZawTennisPlayer(String playerName,String country,int rank, int age,int wins,int losses)
{
this.playerName=playerName;
this.country=country;
this.rank=rank;
this.age=age;
this.wins=wins;
this.losses=losses;

}

//all accesor and mutator method for all six fields.

public String getPlayerName()

{

return playerName;

}

public void setPlayerName(String playerName)

{

this.playerName = playerName;

}

public String getCountry()

{

return country;

}

public void setCountry(String country)

{

this.country = country;

}

public int getRank()

{

return rank;

}

public void setRank(int rank)

{

this.rank = rank;

}

public int getAge()

{

return age;

}

public void setAge(int age)

{

this.age = age;

}

public int getWins()

{

return wins;

}

public void setWins(int wins)

{

this.wins = wins;

}

public int getLosses()

{

return losses;

}

public void setLosses(int losses)

{

this.losses = losses;

}

//method to display player details
public void displayPlayer()
{

System.out.println("Player's name: " + getPlayerName());

System.out.println("Player's country: " + getCountry());

System.out.println("Player's rank: " + getRank());

System.out.println("Player's age: " + getAge());

System.out.println("Player's wins: " + getWins());

System.out.println("Player's losses: " + getLosses());

System.out.println();
}

}

However, when this program compiles but I'm getting the error that "This class does not have a static void main method accepting String[]." when I run it. I know I'm supposed to add some "public static void main(String[] args)" somewhere in the ZawTennisPlayer program but I'm ont sure where. Any idea how I can fix the program to get the desired output?? Thanks in advance!

The sample code "TestTennisPlayer2" I'm using to test the program out is:

public class TestTennisPlayer2
{
  public static void main(String[] args)
  {
    ZawTennisPlayer tp1 = new ZawTennisPlayer();
    ZawTennisPlayer tp2 = new ZawTennisPlayer("Nick Kyrgios", "Australia");
    ZawTennisPlayer tp3 = new ZawTennisPlayer("Simona Halep", "Romania", 1, 26);
    ZawTennisPlayer tp4 = new ZawTennisPlayer("Novak Djokovic", "Serbia", 18, 30, 6, 6);

    tp1.displayPlayer();
    tp2.displayPlayer();
    tp3.displayPlayer();
    tp4.displayPlayer();




  }
}

Upvotes: 0

Views: 83

Answers (1)

irfan
irfan

Reputation: 896

as mentioned in the comments, It seems you are trying to run ZawTennisPlayer where as you should run TestTennisPlayer2
If you are running via command prompt use

>javac TestTennisPlayer2.java

>java TestTennisPlayer2 

or from eclipse IDE ,

open TestTennisPlayer2.java, right click -> run as > java application

Upvotes: 2

Related Questions