Home > Net >  Java: Method looping when there is no loop
Java: Method looping when there is no loop

Time:12-17

I am trying to program a small text rpg. However, when creating the character, it just loops the same question "How would you like to be called?" from the CharCreation class over and over again. How do I fix this?

Also I am pretty new to Java, so please don't be harsh.

public class CharCreation {

    HashMap<String, Player> players = new HashMap<>();
    Rooms roomforcreation = new Rooms();

    public Player mccreate(){
        System.out.println("How would you like to be called?");
        Scanner sc1 = new Scanner(System.in);
        String input1 = sc1.nextLine();
        Player pc = new Player(input1, roomforcreation, 100, 50);
        players.put(pc.getName(), pc);
          if (!players.isEmpty()) {
            Player playerOne = players.entrySet().iterator().next().getValue();
            System.out.println("Welcome, "   playerOne.getName());
            return playerOne;
        }
       return null;
    }
}

This is my CharCreation class, it gets called by the MainGame class when the user tries to start a new game.

public class MainGame implements java.io.Serializable {

    Rooms testroom1 = new Rooms();

    private Player playerOne;

    public void GameLoop() {

        while (true) {

            Scanner in = new Scanner(System.in);
            String input = in.nextLine();

            switch (input) {
                case "save":
                    Mechanics.saveGame();
                    break;
                case "help":
                    HelpMenu hm = new HelpMenu();
                    hm.help();
                    break;
                case "load":
                    MainGame loadedGame = Mechanics.loadGame();
                    playerOne = loadedGame.playerOne;
                    break;
                case "q":
                    System.exit(0);
                    break;
                case "start":
                    CharCreation mc = new CharCreation();
                    playerOne = mc.mccreate();
                    break;
                case "namecheck":
                    if (playerOne != null) {
                        System.out.println(playerOne.getName());
                    } else {
                        System.out.println("Please start or load a new game first!");
                    }
                    break;
                case "checkroom":
                    if (playerOne != null) {
                        System.out.println("You are currently in room "   playerOne.getCurroom()   ".");
                    } else {
                        System.out.println("Please start or load a new game first!");
                    }
                    break;
                case "go":
                    goToRoom();
                    break;
            }
        }
    }
    public void goToRoom() {
        System.out.println("Which room would you like to go to?");
        Scanner sc1 = new Scanner(System.in);
        int roomInput = sc1.nextInt();
        switch (roomInput) {
            case 0:
        try {
            if (playerOne.getCurroom() != playerOne.getCurroom()) {
                    playerOne.setCurroom(testroom1);
                    System.out.println("You are now in Room "   testroom1);
                } else{
                    System.out.println("You are already in this room!");
                }
            } catch(NullPointerException e){
                System.out.println("You must create a character or load a game first!");
            }
            break;
            default:
                System.out.println("Couldn't find this room!");
                break;
        }
    }
}

This is the MainGame class mentioned earlier.

public class Player implements java.io.Serializable {

    Rooms currentRoom = new Rooms();

    private String name;
    private int Health;
    private int Strength;
    private Rooms currentroom;

    public Player() {
    }

    public Player(String initialName, Rooms initialRoom, int initialHealth, int initialStrength) {
        setName(initialName);
        setHp(initialHealth);
        setStr(initialStrength);
        setCurrentRoom(initialRoom);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Rooms getCurroom() {
        return currentroom;
    }

    public void setCurroom(Rooms curroom) {
        this.currentroom = curroom;
    }

    public int getHp() {
        return Health;
    }

    public void setHp(int hp) {
        this.Health = hp;
    }

    public int getStr() {
        return Strength;
    }

    public void setStr(int str) {
        this.Strength = str;
    }
    public Rooms getCurrentRoom() {
        return currentRoom;
    }

    public void setCurrentRoom(Rooms currentRoom) {
        this.currentRoom = currentRoom;
    }

}

And, finally, this is my Player class, which CharCreation tries to create a new instance of.

Looking forward to helping answers!

CodePudding user response:

It would help if you posted the output of your program. But I suspect an exception occurs and you need to clear the buffer.

I would add sc1.nextLine() in the catch clause.

        try {
            if (playerOne.getCurroom() != playerOne.getCurroom()) {
                    playerOne.setCurroom(testroom1);
                    System.out.println("You are now in Room "   testroom1);
                } else{
                    System.out.println("You are already in this room!");
                }
            } catch(NullPointerException e){
                sc1.nextLine(); /// <=== clear buffer 
                System.out.println("You must create a character or load a game first!");
            }

CodePudding user response:

Well, I copied your code, ran it and wrote at stdin "start" and after that "player name". I got "Welcome, player name". So, you obviously need to edit your question.

  •  Tags:  
  • java
  • Related