Question
I am struggling with this java program, the output for some reason is showing extra 0 and i cant figure out why or where the
I am struggling with this java program, the output for some reason is showing extra "0" and i cant figure out why or where the problem is. i have the code done i just need to show the output without the zeros..its probably something simple i just cant figure it out.. the output should just pick up the name, score and shirt. Can anyone help?? thank you!!(u need all 3 files to run it+ text file with the players info. )
Player.Java
public class Player
{ private String Name;
private int Score;
private int Shirt;
public Player() {
Name = "";
Score = 0;
Shirt = 0; }
public Player(String newName, int newScore, int newShirt) {
this.Name=newName;
this.Score=newScore;
this.Shirt=newShirt; }
public Player(Player P) {
this.Name=P.Name;
this. Score=P.Score;
this.Shirt=P.Shirt; }
public void setName(String name) {
Name=name; }
public String getName() {
return Name; }
public void setScore(int scr) {
Score=scr; }
public int getScore() {
return Score;}
public void setShirt(int shrt) {
Shirt=shrt; }
public int getShirt(){
return Shirt; }
@Override
public String toString() {
return "Name=" + Name + ", Score=" + Score + ", Shirt=" + Shirt; }}
PlayerDatabase.Java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class PlayerDatabase
{ private Player[] players;
private int numPlayers;
int index=0;
private int num;
public PlayerDatabase( int numPlayers)
{
this.numPlayers = num;
players=new Player[numPlayers];
} public void AddPlayer(){
Scanner input=new Scanner(System.in);
if(numPlayers
{ players[numPlayers]=new Player();
System.out.println("Enter the player name:");
players[numPlayers].setName(input.next());
System.out.println("Enter the player Score:");
players[numPlayers].setScore(input.nextInt());
System.out.println("Enter the player Shirt:");
players[numPlayers].setShirt(input.nextInt());
numPlayers++; }
else{ System.out.println(" Error");
} }
public void AddPlayer(String PlayersFile)
{ String line;
try
{ FileReader fileReader = new FileReader(PlayersFile);
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
while ((line = bufferedReader.readLine()) != null) {
players[numPlayers]=new Player();
String temp[]=line.split(",");
players[numPlayers].setName(temp[0]);
numPlayers++;
} } }
catch(IOException e) //if not found/error
{ System.out.println("Error; cant read file'"+ PlayersFile + "'"); }}
public void DisplayPlayers(Player[] players)
{ for(int i=0;i { System.out.println(players[i].getName()+" "+players[i].getScore()+" "+players[i].getShirt());} } public void SearchPlayersByName(String playerName) { int index=0; for(int i=0;i {if(playerName.equals(players[i].getName())) { index=i; break; } } System.out.println(players[index].getName()+" "+players[index].getScore()+" "+players[index].getShirt()); } public void SearchPlayersByshirt(int playerShirtNumber) { int index=0; for(int i=0;i if(playerShirtNumber== players[i].getShirt()) { index=i; break;}} System.out.println(players[index].getName()+" "+players[index].getScore()+" "+players[index].getShirt());} public void SearchPlayersByScore(int playerScore) {for(int i=0;i if(playerScore== players[i].getScore()) { System.out.println(players[i].getName()+" "+players[i].getScore()+" "+players[i].getShirt()); }}} public void RemovePlayerByName(String name) { int index=0; for(int i=0;i { if(name.equals( players[i].getName())) { index=i; break;}} for(int j=index;j { players[j]=players[j+1];} numPlayers--;} public void DisplayPlayers() {System.out.println("***Players-Data base***"); for(int i=0;i System.out.println(players[i].getName()+" "+players[i].getScore()+" "+players[i].getShirt()); }}} PlayerDatabaseDemo.Java import java.util.Scanner; public class PlayerDatabaseDemo { public static void main(String[] args) { Scanner input=new Scanner(System.in); String tempName; Player p1 = new Player("Sam",20,1); Player p2 = new Player(p1); p1.setName("John"); System.out.println(p1.getName().equals(p2.getName())); PlayerDatabase Sim= new PlayerDatabase(10); Sim.AddPlayer("players.txt"); Sim.DisplayPlayers(); while(true){ System.out.println("**Menu** 1.Add aplayer 2.SearchPlayersByName 3.SearchPlayersByshirt 4.SearchPlayersByScore 5.RemovePlayerByName 6.DisplayPlayers 7.Exit Enter your choice:"); switch(input.nextInt()) { case 1: Sim.AddPlayer();break; case 2: System.out.println("Enter the player Name:"); tempName=input.next(); Sim.SearchPlayersByName(tempName);break; case 3:System.out.println("Enter the player shirt number:"); int tempShirt=input.nextInt(); Sim.SearchPlayersByshirt(tempShirt);break; case 4:System.out.println("Enter the player score to search:"); int tempScore=input.nextInt(); Sim.SearchPlayersByScore(tempScore);break; case 5:System.out.println("Enter the player Name:"); tempName=input.next(); Sim.RemovePlayerByName(tempName); break; case 6: Sim.DisplayPlayers();break; case 7:System.exit(0); //close default: System.out.println("Enter correct choice"); break; } Sim.DisplayPlayers(); }}} Players.txt "John 20 1" "Walter 402" "David 45 10" "Sam 0 20" "Little 0 22"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started