0

hi so im making a game for uni in java but im still pretty new

import java.util.Scanner;
public class Assignment{ 
public static void main ( String[] args ) {




    Assignment game;
    game = new Assignment();

    game.darkJungle();
    game.oldBridge();
    game.abandonedShack();
}
public void darkJungle() {

Scanner scanner = new Scanner(System.in);   

    System.out.println("Hello adventurer. You awake from your slumber 
in a dark jungle, with no memory of your past life.");
    System.out.println(""); 


    System.out.println("Before you set off exploring for answers, what 
do you wish to call yourself?");
    String playerName = scanner.next();

    System.out.println("hello " + playerName); 
    System.out.println();
    System.out.println("Inventory");
    System.out.println("-------------");
    String weapon;
    weapon = "glock";
    boolean rustyKey = true;


    System.out.println(weapon);
    System.out.println("rustyKey");
    System.out.println();
    System.out.println("You look to the east and west and notice 
they're the safest looking paths. Which path do you set off on?"); 
    System.out.println("1: west");
    System.out.println("2: east");

    int choice;
    choice = scanner.nextInt();


    if(choice==1){
        System.out.println("You have wandered onto a creaky old 
bridge.");
        scanner.nextLine();
        oldBridge();}

    if(choice==2) 
        {System.out.println("You find an abandoned shack.");
        scanner.nextLine();
        abandonedShack();}  


}
public void oldBridge() {
System.out.println(""); 
System.out.println("hi");   

}
public void abandonedShack() {}}

when i run the code it works fine until i get to the IF statements where i have the choice to enter "1" or "2" and the result displays the next println twice? Im still new to java so any help would be appreciated.

  • What result `println twice`? What's the expect ? Please give more details – PatrickChen May 18 '20 at 06:50
  • 1
    You should indent your code and braces properly - makes it easier to see code blocks (and potential bugs) – assylias May 18 '20 at 06:51
  • Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Arvind Kumar Avinash May 18 '20 at 07:03

2 Answers2

0

With the choice = 1 you invoke oldBridge and in the main() you invoke oldBridge again. Is that the

result displays the next println twice

You are talking about?

0

It's a buffer error. After you take choice = scanner.nextInt(); , input a space gets left out, so after the above code simply put choice = scanner.nextLine();. it will work.