How to create only one Scanner object throughout my program

Jacky :

How to create only one Scanner object throughout my program because automatic test will cause your program to generate exceptions and terminate. The reason is that in the automatic test, multiple lines of test inputs are sent all together to the program. It will cause the error.Exception in thread "main" java.util.NoSuchElementException

import java.util.Scanner;


public class NimPlayer
{
    public NimPlayer()
    {

    Scanner Keyboard=new Scanner(System.in);
    System.out.println("Please enter Player 1's name:"  );
    String NameofPlayer1=Keyboard.nextLine();
    System.out.println();


    System.out.println("Please enter Player 2's name:"  );
    String NameofPlayer2=Keyboard.nextLine();
    System.out.println();


    do {

    System.out.println("Please enter upper bound of stone removal:");
    int Upperbound=Keyboard.nextInt();
    System.out.println();


    System.out.println("Please enter initial number of stones:");
    int InitialNum=Keyboard.nextInt();


    removeStone(NameofPlayer1,NameofPlayer2,InitialNum,Upperbound);

    System.out.println();
    System.out.println("Do you want to play again(Y/N):");
    }while(Keyboard.next().equalsIgnoreCase("Y"));
}


    public static void removeStone(String NameofPlayer1,String NameofPlayer2,int InitialNum,int Upperbound)
    {
        Scanner Keyboard1=new Scanner(System.in);

        int Roundplayer=1;

        while(InitialNum>0)
        {
            System.out.println();           
            System.out.print(InitialNum+" "+ "stones left:");
            Star(InitialNum);
            System.out.println();

            if(Roundplayer>0)
            {       
                System.out.println(NameofPlayer1+ "'s turn-remove how many?");
                int NumofPlayer1=Keyboard.nextInt();

                while(NumofPlayer1>Upperbound)
                {
                    System.out.println("Larger than upper bound!" +"\nPlease enter it again\n");
                    break;
                }

                Roundplayer=Roundplayer-1;
                InitialNum=InitialNum-NumofPlayer1;
            }

            else 
            {
                System.out.println(NameofPlayer2+ "'s turn-remove how many?");
                int NumofPlayer2=Keyboard1.nextInt();
                Roundplayer=Roundplayer+1;
                InitialNum=InitialNum-NumofPlayer2;
                //System.out.print(InitialNum+ "stones left:");
                //Star(InitialNum);
            }
        }

        if(Roundplayer>0)
        {
            System.out.println();
            System.out.println("Game Over");
            System.out.println(NameofPlayer1+ " "+"wins");
        }
        else {
            System.out.println();
            System.out.println("Game Over");
            System.out.println(NameofPlayer2+ " "+"wins!");
        }   
    }


    public static void Star(int InitialNum )
    {
        for(int i=0; i<InitialNum; i++ )
        {
            System.out.printf(" "+"*");
        }
    }
}
Arvind Kumar Avinash :

Remove creating the Scanner from the constructor and other methods. Put it inside the class as an instance variable and use it inside the constructor and other methods.

public class NimPlayer {
    Scanner keyboard = new Scanner(System.in);
    public NimPlayer() {
        // You can use keyboard here
    }

    public static void removeStone(String NameofPlayer1,String NameofPlayer2,int InitialNum,int Upperbound) {
        // You can use keyboard here
    }

    //...Other parts of the program
}

I also recommend you follow Java naming convention e.g. your variable name, Keyboard should be keyboard.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=393510&siteId=1