Home While and Try Catch Loop Error Java
Reply: 0

While and Try Catch Loop Error Java

user646
1#
user646 Published in May 21, 2018, 3:02 am

I'm trying to create a simple player selection loop, but when if the user attempts to input something that isn't an integer, the loop "falls throw" in a sense. I don't know what I need to do so this code doesn't fall throw in case of an inappropriate input.

int player;
boolean playerCreated = false;
while (playerCreated == false) {
  try {
    player = scan.nextInt();
      if (player != 1 && player != 2) {
        throw new Exception();
      }
    playerCreated = true;
  }
catch (Exception e) {
  System.out.println("Please input a valid player selection. (1 or 2)");
  continue;
  }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.304413 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO