oh by the way world, have a look at these,
public class Sample {
private static enum Option {YES, NO}
private Scanner scanner;
private int gameNumber;
private static int DEFAULT_NUMBER = 10;
public static void main(String[] args) {
Sample samp = new Sample();
samp.start();
}
public Sample() {
scanner = new Scanner(System.in);
}
private void start() {
Option opt;
opt = play(); // prompt the user if he wants to play the game
System.out.print("\n");
setTheNumber(); // ask the user if he wants to set the game number
while (opt == Option.YES) {
// this is where the game take place
// NOT YET IMPLEMENTED
opt = play(); // this will ask the user if he wants to play again
}
}
private Option play() {
String input;
Option op;
System.out.print("Play? (Yes - y, No - n): ");
input = scanner.next();
if (input.equals("Y") || input.equals("y")) {
op = Option.YES;
}
else {
op = Option.NO;
}
return op;
}
private void setTheNumber() {
String response;
System.out.print("Set The Number: ");
response = scanner.nextLine();
if (response.equalsIgnoreCase("y")) {
System.out.print("Enter Your Desired Number: ");
int num = scanner.nextInt();
gameNumber = num;
}
else {
System.out.println("Default" + DEFAULT_NUMBER);
gameNumber = DEFAULT_NUMBER;
}
}
}
this has a different error.. but someone simply told me to do this
System.out.print("Set The Number: ");
response = scanner.next(); // its in the part of the .next() method
he says i have to change that one into this one Otherwise it's going to look for the \n.
i think there is something to do with the methods of the scanner class, unlike BuffferedReader,
something about buffer reading... (i dont know much about buffers yet)
anyway the error of this program is that
it Skips a block of a statement(in the setTheNumber() method).
i just want to have some clarification of this kind of erros because i've been encountering this kind of output(errors) since i've been using the
scanner class, and when i write a complex program with lots of code, im having a hard time looking at the part where this error took place