kylejustinward
kylejustinward

Reputation: 33

Java Scanner is requiring multiple entries

The following is some code I wrote in Java for setting up a player in a game. The scanners I set up for atk, def, and hp are requiring 2-3 entries. I apologize for the messy code, I am just beginning to learn Java.

String NAME;
int ATK;
int DEF;
int HP;
int remainingPoints = 10;
// pick name
System.out.println("Enter name:");
Scanner p2Name = new Scanner(System.in);
NAME = p2Name.nextLine();
// pick type
System.out.println("Choose element theme: (water,fire,ice)");
Scanner p2Type = new Scanner(System.in);
String TYPE = p2Type.nextLine();
// atk
System.out.println("You have ten points to disperse " + "among your atk, def and hp");
System.out.println("Enter atk:");
Scanner p2Atk = new Scanner(System.in);
if (p2Atk.nextInt() <= 10) {
    ATK = p2Atk.nextInt();
    remainingPoints = remainingPoints - ATK;
} else {
    System.out.println("Your entry was invalid, your " + "atk is now five by default.");
    ATK = 5;
    remainingPoints = remainingPoints - ATK;
}
// def
System.out.println("You now have " + remainingPoints + ". Please enter def:");
Scanner p2Def = new Scanner(System.in);
if (p2Def.nextInt() <= remainingPoints) {
    remainingPoints = remainingPoints - p2Def.nextInt();
    DEF = p2Def.nextInt();
} else {
    System.out.println("Your entry was invalid, your " + "def is now one by default.");
    DEF = 1;
    remainingPoints = remainingPoints - DEF;
}
// hp
System.out.println("You now have " + remainingPoints + ". Please enter hp:");
Scanner p2Hp = new Scanner(System.in);
if (p2Hp.nextInt() <= remainingPoints) {
    remainingPoints = remainingPoints - p2Hp.nextInt();
    HP = p2Hp.nextInt();
} else {
    System.out.println("Your entry was invalid, your " + "hp is now one by default.");
    HP = 1;
    remainingPoints = remainingPoints - HP;
}
Player p2 = new Player(NAME, TYPE, ATK, DEF, HP);
System.out.println(p2.n);
System.out.println(p2.type);
System.out.println(p2.atk);
System.out.println(p2.def);
System.out.println(p2.hp); 

Upvotes: 2

Views: 160

Answers (2)

user3437460
user3437460

Reputation: 17474

There is no need to create a new Scanner object every time you prompt for an input.

The data type you are prompting to input, be it an int, double or String, you only need to create the Scanner object once - i.e.: for system.in.

However beginners in Java may encounter a problem when using scn.nextInt(). You may place a scn.nextLine() after prompting to solve that problem.

But personally I prefer to receive every input as String using scn.nextLine(). If it is an int input, just do:

int num = Integer.parseInt(scn.nextLine());

Example:

Scanner scn = new Scanner(System.in);

String name = scn.nextLine();
String type = scn.nextLine();
String spell = scn.nextLine();
int health = Integer.parseInt(scn.nextLine());

Upvotes: 2

Rajesh
Rajesh

Reputation: 2155

You have created too many Scanner objects which is not right.

About requiring 2-3 entries - you should put value in a temp variable once and then use it n number of times.

PFB corrected code:

String NAME = null;
String TYPE = null;
int ATK = 0;
int DEF = 0;
int HP = 0;
int remainingPoints = 10;

try {

    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter name:");
    NAME = scanner.nextLine();

    System.out.println("Choose element theme: (water,fire,ice)");
    TYPE = scanner.nextLine();

    System.out.println("You have ten points to disperse "
            + "among your atk, def and hp");

    System.out.println("Enter atk:");
    int temp = scanner.nextInt();

    if (temp <= 10)
        ATK = temp;
    else {
        System.out.println("Your entry was invalid, your "
                + "atk is now five by default.");
        ATK = 5;
    }

    remainingPoints = remainingPoints - ATK;
    System.out.println("You now have " + remainingPoints
            + ". Please enter def:");           

    temp = scanner.nextInt();
    if (temp <= remainingPoints)
        DEF = temp;
    else {
        System.out.println("Your entry was invalid, your "
                + "def is now one by default.");
        DEF = 1;
    }
    remainingPoints = remainingPoints - DEF;
    System.out.println("You now have " + remainingPoints
            + ". Please enter hp:");

    temp = scanner.nextInt();
    if (temp <= remainingPoints)
        HP = temp;
    else {
        System.out.println("Your entry was invalid, your "
                + "hp is now one by default.");
        HP = 1;
    }
    remainingPoints = remainingPoints - HP;

    scanner.close();
} catch (Exception e) {
    e.printStackTrace();
}

Player p2 = new Player(NAME, TYPE, ATK, DEF, HP);

System.out.println(p2.n);
System.out.println(p2.type);
System.out.println(p2.atk);
System.out.println(p2.def);
System.out.println(p2.hp);

Upvotes: 1

Related Questions