I have a problem with my code in java, replit. When I run my code in the console, the output for the csv file is always the random ID number, null, 0,0,0. I don't want null, 0,0,0. null,0,0,0 reflects name, totalgames, totalguesses, and bestgame. When I run my program it doesn't give the actual value of totalgames, totalguesses, and bestgame just 0,0,0 every time. Could you fix this? Also name is null because I haven't asked for the player's name, so I would like you to include that as well in this code. Here is the code, each class in seperate files in replit: import java.util.Scanner; import java.util.Random; public class Main { public static void main(String[] args) { Scanner console = new Scanner(System.in); Random random = new Random(); Game game = new Game(console, random); game.startGame(); console.close(); } } import java.util.Scanner; import java.util.UUID; public class Player { private UUID id; private String name; private int totalGames; private int totalGuesses; private int bestGame; public Player() { this.id = UUID.randomUUID(); } public UUID getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTotalGames() { return totalGames; } public void setTotalGames(int totalGames) { this.totalGames = totalGames; } public int getTotalGuesses() { return totalGuesses; } public void setTotalGuesses(int totalGuesses) { this.totalGuesses = totalGuesses; } public int getBestGame() { return bestGame; } public void setBestGame(int bestGame) { this.bestGame = bestGame; } public int playGame(Scanner console, int answer) { System.out.println("I'm thinking of a number between 1 and " + Game.MAX + "..."); int guesses = 0; while (true) { System.out.print("Your guess? "); if (console.hasNextInt()) { int guess = console.nextInt(); guesses++; if (guess == answer) { System.out.println("You got it right in " + guesses + " guess" + (guesses == 1 ? "!" : "es!")); break; } else if (guess < answer) { System.out.println("It's higher."); } else { System.out.println("It's lower."); } } else { System.out.println("Invalid input. Please enter a valid integer."); console.next(); // consume invalid input } } return guesses; } } import java.io.*; import java.util.Scanner; import java.util.Random; public class Game { public static final int MAX = 100; private final Scanner console; private final Random random; private static final String CSV_FILE_PATH = "player_results.csv"; public Game(Scanner console, Random random) { this.console = console; this.random = random; } public void startGame() { int totalGames = 0; int totalGuesses = 0; int bestGame = Integer.MAX_VALUE; while (true) { Player player = new Player(); int answer = 1 + random.nextInt(MAX); int guesses = player.playGame(console, answer); totalGames++; totalGuesses += guesses; bestGame = Math.min(bestGame, guesses); // Save player information to CSV savePlayerToCSV(player); System.out.println("Play again y/n?"); String playAgain = console.next().toLowerCase(); if (!playAgain.startsWith("y")) { break; } } printOverallStatistics(totalGames, totalGuesses, bestGame); } private void printOverallStatistics(int totalGames, int totalGuesses, int bestGame) { System.out.println("Total games: " + totalGames); System.out.println("Total guesses: " + totalGuesses); System.out.println("Guesses/game: " + (totalGames == 0 ? 0 : totalGuesses / totalGames)); System.out.println("Best game: " + bestGame); } private void savePlayerToCSV(Player player) { try (FileWriter writer = new FileWriter(CSV_FILE_PATH, true); BufferedWriter bw = new BufferedWriter(writer); PrintWriter out = new PrintWriter(bw)) { out.println(player.getId() + "," + player.getName() + "," + player.getTotalGames() + "," + player.getTotalGuesses() + "," + player.getBestGame()); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Scanner console = new Scanner(System.in); Random random = new Random(); Game game = new Game(console, random); game.startGame(); console.close(); } }
I have a problem with my code in java, replit. When I run my code in the console, the output for the csv file is always the random ID number, null, 0,0,0. I don't want null, 0,0,0. null,0,0,0 reflects name, totalgames, totalguesses, and bestgame. When I run my program it doesn't give the actual value of totalgames, totalguesses, and bestgame just 0,0,0 every time. Could you fix this? Also name is null because I haven't asked for the player's name, so I would like you to include that as well in this code.
Here is the code, each class in seperate files in replit:
import java.util.Scanner;
import java.util.Random;
public class Main {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        Random random = new Random();
        Game game = new Game(console, random);
        game.startGame();
        console.close();
    }
}
import java.util.Scanner;
import java.util.UUID;
public class Player {
  private UUID id;
  private String name;
  private int totalGames;
  private int totalGuesses;
  private int bestGame;
  public Player() {
      this.id = UUID.randomUUID();
  }
  public UUID getId() {
      return id;
  }
  public String getName() {
      return name;
  }
  public void setName(String name) {
      this.name = name;
  }
  public int getTotalGames() {
      return totalGames;
  }
  public void setTotalGames(int totalGames) {
      this.totalGames = totalGames;
  }
  public int getTotalGuesses() {
      return totalGuesses;
  }
  public void setTotalGuesses(int totalGuesses) {
      this.totalGuesses = totalGuesses;
  }
  public int getBestGame() {
      return bestGame;
  }
  public void setBestGame(int bestGame) {
      this.bestGame = bestGame;
  }
  
    public int playGame(Scanner console, int answer) {
        System.out.println("I'm thinking of a number between 1 and " + Game.MAX + "...");
        int guesses = 0;
        while (true) {
            System.out.print("Your guess? ");
            if (console.hasNextInt()) {
                int guess = console.nextInt();
                guesses++;
                if (guess == answer) {
                    System.out.println("You got it right in " + guesses + " guess" + (guesses == 1 ? "!" : "es!"));
                    break;
                } else if (guess < answer) {
                    System.out.println("It's higher.");
                } else {
                    System.out.println("It's lower.");
                }
            } else {
                System.out.println("Invalid input. Please enter a valid integer.");
                console.next(); // consume invalid input
            }
        }
        return guesses;
    }
}
import java.io.*;
import java.util.Scanner;
import java.util.Random;
public class Game {
    public static final int MAX = 100;
    private final Scanner console;
    private final Random random;
    private static final String CSV_FILE_PATH = "player_results.csv";
    public Game(Scanner console, Random random) {
        this.console = console;
        this.random = random;
    }
    public void startGame() {
        int totalGames = 0;
        int totalGuesses = 0;
        int bestGame = Integer.MAX_VALUE;
        while (true) {
            Player player = new Player();
            int answer = 1 + random.nextInt(MAX);
            int guesses = player.playGame(console, answer);
            totalGames++;
            totalGuesses += guesses;
            bestGame = Math.min(bestGame, guesses);
            // Save player information to CSV
            savePlayerToCSV(player);
            System.out.println("Play again y/n?");
            String playAgain = console.next().toLowerCase();
            if (!playAgain.startsWith("y")) {
                break;
            }
        }
        printOverallStatistics(totalGames, totalGuesses, bestGame);
    }
    private void printOverallStatistics(int totalGames, int totalGuesses, int bestGame) {
        System.out.println("Total games: " + totalGames);
        System.out.println("Total guesses: " + totalGuesses);
        System.out.println("Guesses/game: " + (totalGames == 0 ? 0 : totalGuesses / totalGames));
        System.out.println("Best game: " + bestGame);
    }
    private void savePlayerToCSV(Player player) {
        try (FileWriter writer = new FileWriter(CSV_FILE_PATH, true);
             BufferedWriter bw = new BufferedWriter(writer);
             PrintWriter out = new PrintWriter(bw)) {
            out.println(player.getId() + "," +
                        player.getName() + "," +
                        player.getTotalGames() + "," +
                        player.getTotalGuesses() + "," +
                        player.getBestGame());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        Random random = new Random();
        Game game = new Game(console, random);
        game.startGame();
        console.close();
    }
}


Step by step
Solved in 3 steps









