Scanner.nextLine(); called before wanted due to loop

I have a scanner input which is called by getName() in my Player class.

public static String getName()
{
  System.out.println("Enter your character's name.");
   String n = input.nextLine();
  return n;
}

However, when I use a loop in order to check for appropriate player input in my RPG class, everything works fine until I enter "n" or "N" for

System.out.println("Confirm Character (Y/N)");
confirm = input.next();

The way the loop works is that it calls the constructor public Player(String cName, int cStamina, int cDefense, int cStrength, int cAgility, int cIntellect) again if "n" or "N" are selected, however, because those two are String data types, and the constructor calls for a name first, it either recognizes the "n" or "N" as the name, or skips the whole name altogether. How do I fix this?

import java.util.*;
public class RPG 
{
  static Scanner input = new Scanner(System.in);
  private static String confirm; 

  public static void main(String[] args)
  {
    System.out.println("*~~~~~~~~~~~~~~*\n{ ~ Pinnacle ~ }\n*~~~~~~~~~~~~~~*");
    for (int s = 0; s < 1; s++)
    {

    Player me = new Player(Player.getName(), Player.getStam(), Player.getDef(),
    Player.getStr(), Player.getAgi(), Player.getInt()); 

      for (int i = 0; i < 1; i++)
      {       
        System.out.println("*~~~~~~~~~~~~~~*\n"+Player.name +
        "\nStamina: " + Player.stamina +
        "\nDefense: " + Player.defense +
        "\nStrength: " + Player.strength +
        "\nAgility: " + Player.agility +
        "\nIntellect: " + Player.intellect +
        "\n*~~~~~~~~~~~~~~*");
        System.out.println("Confirm Character (Y/N)");
        confirm = input.next();
        if (confirm.equals("Y") || confirm.equals("y"))
          System.out.println("Character Created!");
        else if (confirm.equals("N") || confirm.equals("n"))
          s--;
        else
          i--;  
      }
  }
  }
}

Player Class below

import java.util.*;

public class Player extends Characters
{

static Scanner input = new Scanner(System.in);

public Player(String cName, int cStamina, int cDefense, int cStrength, int cAgility, int cIntellect)
{


name = cName;
stamina = cStamina;
defense = cDefense;
strength = cStrength;
agility = cAgility;
intellect = cIntellect;
}
public static String getName()
{
System.out.println("Enter your character's name.");
String n = input.nextLine();
return n;
}
public static int getStam()
{
System.out.println("Enter your character's stamina.");
int s = input.nextInt();
return s;
}
public static int getDef()
{
System.out.println("Enter your character's defense.");
int d = input.nextInt();
return d;
}
public static int getStr()
{
System.out.println("Enter your character's strength.");
int st = input.nextInt();
return st;
}
public static int getAgi()
{
System.out.println("Enter your character's agility.");
int a = input.nextInt();
return a;
}
public static int getInt()
{
System.out.println("Enter your character's intellect.");
int i = input.nextInt();
return i;
}
public static void flee()
{

}

}

Here is a sample output with Sample Name as the first name input:

*~~~~~~~~~~~~~~*
{ ~ Pinnacle ~ }
*~~~~~~~~~~~~~~*
Enter your character's name.
Sample Name
Enter your character's stamina.
5
Enter your character's defense.
5
Enter your character's strength.
8
Enter your character's agility.
3
Enter your character's intellect.
2
*~~~~~~~~~~~~~~*
Sample Name
Stamina: 5
Defense: 5
Strength: 8
Agility: 3
Intellect: 2
*~~~~~~~~~~~~~~*
Confirm Character (Y/N)
n
Enter your character's name.
Enter your character's stamina.
2
Enter your character's defense.
3
Enter your character's strength.
6
Enter your character's agility.
4
Enter your character's intellect.
2
*~~~~~~~~~~~~~~*

Stamina: 2
Defense: 3
Strength: 6
Agility: 4
Intellect: 2
*~~~~~~~~~~~~~~*
Confirm Character (Y/N)
y
Character Created!

As you can see, the problems arise after Confirm Character (Y/N).

Answers


Well, I found out a solution for your problem, I'm not sure why your problem is happening. Instead of using static Scanner input = new Scanner(System.in); in our Player class, create a new Scanner in every your gets*.

Here is a suggestion:

public static Player createPlayerFromConsole() {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter your character's name.");
    String name = input.nextLine();

    System.out.println("Enter your character's stamina.");
    int stamina = input.nextInt();

    System.out.println("Enter your character's defense.");
    int defense = input.nextInt();

    System.out.println("Enter your character's strength.");
    int strength = input.nextInt();

    System.out.println("Enter your character's agility.");
    int agility = input.nextInt();

    System.out.println("Enter your character's intellect.");
    int intellect = input.nextInt();

    return new Player(name, stamina, defense, strength, agility, intellect);
}

Need Your Help

Ng-click ng-show/ng-hide for a list of buttons created by ng-repeat

angularjs angularjs-ng-repeat ng-show ng-hide

I have a list of play/pause buttons created by an ng-repeat. You click each play button to stream a song and the button turns into a pause button (so you can then pause it). I was able to get the

Keep file between http requests

java ajax file-upload spring-mvc

Imagine this simple form for uploading a file: