Java 'medieval serilazation' project comes up with " '(' or '[' expected" bug on my return new player; statement

Please help I have no clue why it wont work
Exact error code:
MedievalGame.java:77: error: ‘(’ or ‘[’ expected
return new player;
^
Code in question:
import java.util.Scanner;
import java.util.Objects;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class MedievalGame {

/* Instance Variables /
private Player player;
/
Main Method */
public static void main(String args) {

Scanner console = new Scanner(System.in);
MedievalGame game = new MedievalGame();

game.player = game.start(console);

game.addDelay(500);
System.out.println("\nLet's take a quick look at you to make sure you're ready to head out the door.");
System.out.println(game.player);

game.addDelay(1000);
System.out.println("\nWell, you're off to a good start, let's get your game saved so we don't lose it.");
game.save();

game.addDelay(2000);
System.out.println("We just saved your game...");
System.out.println("Now we are going to try to load your character to make sure the save worked...");

game.addDelay(1000);
System.out.println("Deleting character...");
String charName = game.player.getName();
game.player = null;

game.addDelay(1500);
game.player = game.load(charName, console);
System.out.println("Loading character...");

game.addDelay(2000);
System.out.println("Now let's print out your character again to make sure everything loaded:");

game.addDelay(500);
System.out.println(game.player);

} // End of main

/* Instance Methods */
private Player start(Scanner console) {
// Add start functionality here
Player player;
Art.homeScreen();
System.out.println(“Welcome to MMOTBRPGFUWJVM, traveller!”);
System.out.println(“Now would you like to start a new game or load an old one?”);
System.out.println(“Y for a saved game N for a new game”);
String answer = console.next().toLowerCase();
while (true) {
addDelay(500);
if (answer.equals(“y”)) {
System.out.print("\nAhh… I knew I remembered you, what was your name again? Let me see if I can find your backpack: “);
player = load(console.next(), console);
break;
} else if (answer.equals(“n”)) {
System.out.print(”\nWell then, don’t be shy, go ahead an tell me your name: ");
String possibleName = console.next();
while (true) {
System.out.println("Welcome " + possibleName + “, am I pronouncing that correctly? (Enter ‘y’ to confirm, ‘n’ to enter a new name”);
String nameResponse = console.next().toLowerCase();
if (Objects.equals(nameResponse, “y”)) break;
System.out.println(“So sorry, can you spell it for me again?”);
possibleName = console.next();
}
player = new Player(possibleName);
break;
} else {
System.out.print("Sorry adventurer, I only speak the common tongue, please enter ‘y’ to load a game or ‘n’ to start a new game: ");
answer = console.next().toLowerCase();
}
}
return new player;
} // End of start

private void save() {
// Add save functionality here
String fileName = player.getName() + “.svr”;
try {
FileOutputStream userSaveFile = new FileOutputStream(fileName);
ObjectOutputStream playerSaver = new ObjectOutputStream(userSaveFile);
} catch (IOException e) {
System.out.println(“Sorry we couldnt save your game properly, please try again”);
}
} // End of save

private Player load(String playerName, Scanner console) {
// Add load functionality here
Player loadedPlayer;
try {
FileInputStream userLoad = new FileInputStream(playerName + “.svr”);
ObjectInputStream userLoader = new ObjectInputStream(userLoad);
loadedPlayer = new Player(userLoader);
} catch (IOException | ClassNotFoundException e) {
addDelay(1500);
System.out.println(“Sorry, your game couldnt be loaded: making new save file now”);
addDelay(2000);
loadedPlayer = new Player(playerName);
}
return loadedPlayer;
} // End of load

// Adds a delay to the console so it seems like the computer is “thinking”
// or “responding” like a human, not instantly like a computer.
private void addDelay(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Hello,
your code is broken because sometimes you used the wrong quotation marks. I assume you copied these parts from the instructions (I also had this problem a few times).
I recommend pasting your code into a local code editor with better syntax highlighting. I used Visual Studio Code. Once you fixed this issue debugging should become a lot easier.

Regarding your return statement you should delete the “new” keyword since “player” is the Player instance you already created.

Hope I could help :slight_smile:

when I get rid of the new keyword I get 6 other errors all for “cannot find symbol” and as for the quotation marks I looked and it seems fine

can you show me what you did because I just cant figure it out

So, here is my MedievalGame class. It was working back then, but I cannot guarantee that it is perfectly correct or that I haven’t forgotten any key features. :upside_down_face:

import java.util.Scanner;
import java.util.Objects;
import java.io.*;


public class MedievalGame {

  /* Instance Variables */
  private Player player;

  /* Main Method */
  public static void main(String[] args) {
    
    Scanner console = new Scanner(System.in);
    MedievalGame game = new MedievalGame();

    game.player = game.start(console);

    game.addDelay(500);
    System.out.println("\nLet's take a quick look at you to make sure you're ready to head out the door.");
    System.out.println(game.player);

    game.addDelay(1000);
    System.out.println("\nWell, you're off to a good start, let's get your game saved so we don't lose it.");
    game.save();

    game.addDelay(2000);
    System.out.println("We just saved your game...");
    System.out.println("Now we are going to try to load your character to make sure the save worked...");

    game.addDelay(1000);
    System.out.println("Deleting character...");
    String charName = game.player.getName();
    game.player = null;

    game.addDelay(1500);
    game.player = game.load(charName, console);
    System.out.println("Loading character...");

    game.addDelay(2000);
    System.out.println("Now let's print out your character again to make sure everything loaded:");

    game.addDelay(500);
    System.out.println(game.player);
  } // End of main

  /* Instance Methods */
  private Player start(Scanner console) {
    // Add start functionality here
    Player player;
    Art.homeScreen();
    System.out.println("Welcome to your latest adventure!");
    System.out.println("Tell me traveler, have you been here before?");
    System.out.print("   Enter 'y' to load a game, 'n' to create a new game: ");
    String answer = "";
    while(true) {
      answer = console.next().toLowerCase();
      if(answer.equals("y")) {
        System.out.println("so, let's load your character!"); 
        player = load(console.next(), console);
        break;
    }
      else if (answer.equals("n")) {
        System.out.println("okay, let's create a new character for you. What's your name?"); 
        String playerName = console.next();
        while (true) {
      System.out.println("You chose " + playerName + "\nSure about that? (Enter 'y' to confirm, 'n' to enter a new name");
      String nameResponse = console.next().toLowerCase();
      if (Objects.equals(nameResponse, "y")) break;
      System.out.println("Choose something else then!");
      playerName = console.next();
    }
    player = new Player(playerName);
        break;
      }
      else {
          System.out.println("'y' or 'n'! Is that so hard?!"); 
      }
    }
    
    return player;
  } // End of start

  private void save() {
    // Add save functionality here
    String fileName = player.getName() + ".svr";
    try {
      FileOutputStream userSaveFile = new FileOutputStream(fileName);
      ObjectOutputStream playerSaver = new ObjectOutputStream(userSaveFile);
      playerSaver.writeObject(player);
    } catch (IOException e) {
      System.out.println("There was an error saving your game, your file might not be available the next time you go to load a game.");
    }
    
  } // End of save

  private Player load(String playerName, Scanner console) {
    // Add load functionality here
    Player loadedPlayer;
    try {
      FileInputStream userLoadFile = new FileInputStream(playerName + ".svr");
      ObjectInputStream playerLoader = new ObjectInputStream(userLoadFile);
      loadedPlayer = (Player) playerLoader.readObject();
    } catch(IOException|ClassNotFoundException e) {
      addDelay(1500);
System.out.println("\nThere was a problem loading your character, we've created a new player with the name you entered.");
System.out.println("If you're sure the spelling is correct, your character file may no longer exist, please reload the game if you'd like to try again.");
System.out.println("In the mean time, we'll create you a new character with the name: " + playerName);
addDelay(2000);
loadedPlayer = new Player(playerName);
    }
    return loadedPlayer;
  } // End of load

  // Adds a delay to the console so it seems like the computer is "thinking"
  // or "responding" like a human, not instantly like a computer.
  private void addDelay(int time) {
    try {
      Thread.sleep(time);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}
1 Like