Results 1 to 2 of 2
  1. #1
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default Unexplained output

    hi!, i got some problem with my program , its like skipping a block of statement inside a method, heres the code
    Java Code:
    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;
            }
        }
    }
    the out put is this
    Java Code:
    Play? (Yes - y, No - n): y
    
    Set The Number: Default10
    Play? (Yes - y, No - n):
    the program should be like this.

    //ask the user if he wants to play
    // ask the user if he wants to set the number, if not use the Default_number

    but the problem is its skipping the method setTheNumber() and immediately goes to the loop where the game will take place..

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Better make it
    Java Code:
                    System.out.print("Set The Number: ");
    		response = scanner.next();
    instead. Otherwise it's going to look for the \n.

Similar Threads

  1. Need help: output is not what i want
    By Joshsmith in forum New To Java
    Replies: 8
    Last Post: 09-28-2009, 10:09 AM
  2. cannot see the output...
    By Mihail Kravsun in forum New To Java
    Replies: 3
    Last Post: 04-15-2009, 06:57 AM
  3. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM
  4. What will be output and why
    By huma in forum Threads and Synchronization
    Replies: 4
    Last Post: 06-26-2008, 10:14 PM
  5. Why the output is always zero
    By mehrotra.chitij in forum New To Java
    Replies: 12
    Last Post: 04-25-2008, 04:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •