0

I have this code that I am trying to run to get input from user through scanner class. And I am not able to type anything in the input console. There are no exceptions. Just the computer is waiting for input and what I type is not showing in the console.

import java.util.HashMap;
import java.util.Scanner;

class DataBreach extends Exception{}

public class tictactoe{
    char[] data = new char[]{' ',' ',' ',' ',' ',' ',' ',' ',' '};
    HashMap<Integer, Character> XO = new HashMap<Integer, Character>();

    void assignplayercharacter(){
        Scanner scan = new Scanner(System.in);
        while (true){
            String xo;
            System.out.print("Enter the player1 character(X or O):");
            while(!(scan.hasNext())){}
            xo = scan.next();
            if((xo.equals("x"))||(xo.equals("o"))||(xo.equals("X"))||(xo.equals("O"))){
                char x_o = xo.toUpperCase().charAt(0);
                XO.put(1,x_o);
                x_o = ((xo.toUpperCase().equals("X"))?'O':'X');
                XO.put(2,x_o);
                break;
            }
        }
        scan.close();
    }

    void play(int player){
        Scanner scan = new Scanner(System.in);
        while (true){
            int pos;
            System.out.print("Player"+player+"'s turn:");
            while(!(scan.hasNext())){}
            try{
                pos = Integer.parseInt(scan.next());
                if(data[pos-1]!=' '){
                    throw new DataBreach();
                }
                data[pos-1] = XO.get(player);
                break;
            }
            catch(java.util.InputMismatchException e){
                System.out.println("Please enter only numbers from 1-9");
                String leftover = scan.nextLine();
            }
            catch(java.lang.ArrayIndexOutOfBoundsException e){
                System.out.println("Please enter numbers only from 1-9");
            }
            catch(DataBreach e){
                System.out.println("That spot is already occupied. Please choose another spot");
            }

        }
        scan.close();
    }

    void game(){
        assignplayercharacter();
        play(1);
    }


    public static void main(String args[]){
        tictactoe obj = new tictactoe();
        obj.game();
    }
}

When I run this program I am able to get input for the first method void assignplayercharacter but for the second function void play(int player). The program is running and the console is waiting for input but, when I type the character is not appearing on the console. If I comment out the first method void assignplayercharacter and just try to run the void play(int player), I am able to read input from the user.

void assignplayercharacter() - This function gets input and enters data into the HashMap.

void play(int player) - This function gets input from the user and modifies the character in char[] data array according to the input given.

P.S I am having problem only in getting input from the user.

3
  • Remove this line and try again : scan.close(); The first one. Commented Mar 20, 2019 at 11:30
  • You should close the scanner just one time even if there are different scanner objetcs. Commented Mar 20, 2019 at 11:32
  • You shouldn't close the scanner at all. Don't close what you didn't open. Commented Mar 20, 2019 at 12:03

2 Answers 2

1

Remove this line and try again : scan.close(); The first one.
The Scanner should closed at the end only.

PS : To make the game work, remove break; inside void play(int player) function.

Sign up to request clarification or add additional context in comments.

Comments

0

The java.util.Scanner.close() method closes the Scanner. You can't take the input from the console if you have closed the Scanner object. So remove the scan.close() from your code and it will work like charm. Only close scanner object at the end. More on Scanner class and its methods

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.