0

Beginner Java enthusiast here.

I wanted to take a break from structured practice stuff and start with a blank page, and no help to write something of my own to test myself.

I had to learn about console().readLine() as I had not learned how to receive user input from the console. This lead me to parsing integers etc. I was able to build, and compile a program that did what I had envisioned, and did so perfectly! (if not a little clumsily...) However, I wanted to think outside the box, and see how I could use it in ways NOT intended and break it. Inputting anything but a number at the console (for instance typing P instead of 6) it throws an error because I only told it how to handle a number.

Long story short, I stretched past my knowledge and taught myself a lot, but could not find the answer I was looking for due to my lack of knowledge. If someone can even just tell me what to read up on, I am more than happy to do the learning myself, just need a nudge in the right direction.

Here is my program:

public class mysteryDoors{
  public static void main(String[] args){
    int doorOne = 1;
    int doorTwo = 2;
    int doorThree = 3;

    System.out.println("Welcome contestant, to the Mystery Doors Game!");
    System.out.println("Would you like to find out what is behind door number 1?");
    System.out.println("Maybe door number 2?");
    System.out.println("Or perhaps, door number 3?!");

    int choice = Integer.parseInt(System.console().readLine("Please Choose a door..." ));

    while (choice > 3){
      System.out.println("I am sorry, please choose one of the available doors.");
      int guess = Integer.parseInt(System.console().readLine("Please try again..." ));
      choice = guess;
    }  

    if (choice == 1){
      System.out.println("Behind door number 1 is a wonderful sofa set!");
    }
    else if (choice == 2){
      System.out.println("Behind door number 2 is a nice shiny silverware set!");
    }
    else if (choice == 3){
      System.out.println("Behind door number 3 is a NEW CAR!");
    }
  }
}
  • What exactly is the error you are receiving/what problem do you need fixed? If you can add a specific question that you have or the errors that you are recieving, as well as the steps to reproduce that would help (the steps to reproduce may well be just enter P instead of a number as you mentioned, but we still need to know the intended behavior). – wolffer-east Sep 25 '14 at 18:51
  • Thank you. When running the code, and using P instead of a number I get an exception thrown because the input is not parse-able to an integer (I am assuming.) Does that make more sense? – TitaniumSparrow Sep 26 '14 at 02:50

2 Answers2

1

Why not use Scanner? Refer to: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Scanner scanner = new Scanner(System.in);
int choice = in.nextInt();

AFAIK System.console() doesn't work in IDEs.

proulxs
  • 498
  • 2
  • 13
0

Use a try/catch block to handle exceptions, like so:

try
{
    int guess = Integer.parseInt(System.console().readLine("Please try again..." ));
    choice = guess;
}
catch(NumberFormatException e)
{
    System.out.println("Sorry, your input should be an integer. Try again.");
    e.printStackTrace();
}
MarsAtomic
  • 10,436
  • 5
  • 35
  • 56