Question
I have the following java-program, that creates a random walk starting at (0,0) and continuing until x or y is greater than abs(n).
First: I get an error, when I try closing the Scanner by inserting "reader.close(); " in line 28. It says "Unreachable code". How can I fix that?
Second: Is it possible to create a different colour for the StdDraw.point when it reaches a point on one of the edges "n+1" or "n-1" ?
Explanation / Answer
Answer First : You miss the else condtion in your while loop which should be execute when n = 0. So when you are not using break statement for while loop then the code after while loop is unreachable. You should give a condition in loop after that program can be continue. I make this for you now reader.close() is reachable. // yout main method public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter the size of grid : "); do { try { int n = reader.nextInt(); if (n < 0) { System.out.println( n + " is not a positive integer. Please enter a apositive integer (0 to terminate): "); } else if (n > 0) { walk(n); System.out.println(" Enter the size of grid: "); } else { break; } } catch (InputMismatchException e) { System.out.println("Input is not an integer. Please enter a positive (0 to terminate):"); reader.nextLine(); } } while (true); reader.close(); } Answer Second : For use color you can use different color for StdDraw.point() example : StdDraw.setPenColor(StdDraw.BLUE); StdDraw.point(n+1, n-1); StdDraw.setPenColor(StdDraw.MAGENTA);