1

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).

Zerukai
  • 137
  • 1
  • 12
  • you could use input.nextLine() instead of input.next() – Rafael Teles Jan 22 '16 at 04:14
  • @RafaelTeles changing `confirm = input.next();` to `confirm = input.nextLine();` does not resolve the issue, I'm afraid. – Zerukai Jan 22 '16 at 04:16
  • Could you post your "Player" class? – Rafael Teles Jan 22 '16 at 04:18
  • I'm interested in your getName() method. Where does it pull the name from? Or maybe something is going wrong with the call to it after the first call? – johnnelm9r Jan 22 '16 at 04:22
  • this is due to the fact that nextInt() does not move the scanner to the next line while nextLine() does. input your ints as following `int stamina = Integer.parseInt(input.nextLine());` and your code should work. – awd Jan 22 '16 at 04:54

1 Answers1

1

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);
}
Rafael Teles
  • 2,708
  • 2
  • 16
  • 32