The following code is supposed to generate random numbers and give some output as long as the user wants to continue. But the problem is it gives an exception if I press 'y'. No problem occurs if I enter 'n'. Kindly check and tell what wrong I am doing:
import java.util.*; import static java.lang.System.out; public class IsaacDice { public static void main(String[] args) { out.println("Welcome to the dice game! Want to play?: Y/N"); Scanner reply=new Scanner(System.in); char answer = reply.findInLine(".").charAt(0); Random value=new Random(); while (answer=='y') { int randomNumber = value.nextInt(4) + 1; switch (randomNumber) { case 1: out.println("Yes. Isn’t it obvious?"); break; case 2: out.println("No, and don’t ask again."); break; case 3: out.print("Yessir, yessir!"); out.println(" Three bags full."); break; case 4: out.print("What part of ‘no’"); out.println(" don’t you understand?"); break; default: out.print("You think you have"); out.print(" problems?"); out.print(" My random number"); out.println(" generator is broken!"); break; } out.println("Goodbye! Want to play again? y/n:"); out.println(); answer = reply.findInLine(".").charAt(0); } out.println("Exiting game!"); } }