Click here to Skip to main content
15,895,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Scanner sc = new Scanner(System.in);
int tosend = sc.nextInt();

I want that user should be able to only enter any one number, after that scanner object should be stopped but i don't want to close this object because i need it later.

What I have tried:

I tried to restrict for entering only one number using flag variable but didnt worked.
Posted
Updated 17-Jul-20 22:09pm
v3
Comments
Richard MacCutchan 18-Jul-20 4:10am    
It is not difficult to write code that only calls nextInt once. What exactly did you try?
nitinjain9619 18-Jul-20 4:43am    
i was trying wrong i created variable int flag=1 and then before reading user input i was checking flag is 1 if yes then allow to write using sc.nextInt(); and after that flag=0;
Richard MacCutchan 18-Jul-20 4:53am    
Why do you need a flag at all? Just call nextInt once to capture the user's input. You do not need to call it again. But you really need to show your code and explain more clearly exactly what you are trying to do.
nitinjain9619 18-Jul-20 5:10am    
actually i want that if user enter for first than it should be allowed, after that scanner object should not store anything (after enter key pressed).
i am working on a project bingo game where each player will get chance circularly,my code is working perfect if user enter one number at a time but whenever he enter two number simultaneously then at next chance whatever he written 2nd time in his previous chance is considering his played number in current chance i want to stop user for entering two number simultaneously.

and thanks for your support..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900