Results 1 to 9 of 9
  1. #1
    Shzylo is offline Member
    Join Date
    Dec 2012
    Location
    Oklahoma, USA
    Posts
    5
    Rep Power
    0

    Question Java User-Input bug (not continuing); NOVICE

    Hello, I hope to be a member here a while, and as my first official post, I would like to issue my novice bug. I know, you will find it extremely easy. I am trying to make a text-adventure as my first step to game programming, and I ended with a bug with user input that doesn't continue the user-input.

    Here is the code:

    Game.java
    Java Code:
    package main;
    
    import java.util.Scanner;
    
    public class Game extends Variables {
    	
    	public Game() {
    	}
    	public static void main(String[] args) {
    		Main.start();
    	}
    }
    Variables.java
    Java Code:
    package main;
    
    import java.util.Scanner;
    
    public class Variables {
    static Scanner i = new Scanner(System.in); 
    	public static String direction = "";
    	
    	public static void sleep() {
    		try {
    			Thread.sleep(1000);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	
    	public static void space() {
    		System.out.println("");
    	}
    }
    Main.java
    Java Code:
    package main;
    
    public class Main extends Variables {
    	
    	public static void start() {
    		System.out.println("You wake up lost, washed ashore on a beach. All you see in sight is a twig, a rock, and lots of sand.");
    		System.out.println("COMMANDS: go \n" + "OPTIONS: North, South, East, West");
    		direction = i.nextLine();
    		
    		if(direction.equals("North")) {
    			north();
    		} else if(direction.equals("north")) {
    			north();
    			
    			
    		} else if(direction.equals("South")) {
    			south();
    		} else if(direction.equals("south")) {
    			south();
    			
    			
    		} else if(direction.equals("East")) {
    			east();
    		} else if(direction.equals("east")) {
    			east();
    			
    			
    		} else if(direction.equals("West")) {
    			west();
    		} else if(direction.equals("west")) {
    			west();
    		}
    	}
    	
    	public static void north() {
    		System.out.println("You have traveled north into a forest, where you see a turtle resting on a rock, nothing special in this direction.");
    		sleep();
    		System.out.println("Please choose an action:");
    		direction = i.nextLine();
    	}
    	public static void south() {
    		System.out.println("I wouldn't go that way, there is an aweful lot of water there.");
    		sleep();
    		System.out.println("Please choose an action:");
    		direction = i.nextLine();
    	}
    	public static void east() {
    		System.out.println("Oh, a snake, it's staring and hissing at you. I would get some defense before I go there.");
    		sleep();
    		System.out.println("Please choose an action:");
    		direction = i.nextLine();
    	}
    	public static void west() {
    		System.out.println("I see rope  and some fish stuck in shore, flopping");
    		sleep();
    		System.out.println("Please choose an action:");
    		direction = i.nextLine();
    	}
    }

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Java User-Input bug (not continuing); NOVICE

    Your start() method needs to have a loop so that the game doesn't just end after the first command is given.

  3. #3
    Shzylo is offline Member
    Join Date
    Dec 2012
    Location
    Oklahoma, USA
    Posts
    5
    Rep Power
    0

    Default Re: Java User-Input bug (not continuing); NOVICE

    What kind of loop? for, while? I am new with loops, so it is hard for me.

  4. #4
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Java User-Input bug (not continuing); NOVICE

    Well, I do-loop would be good because a do-loop is executed at least once. We want the user to be able to execute at least one command. If the user enters "North", "South", "East", or "West", you would execute that command and then let the loop get the next command. If the input is not "North", "South", "East", or "West", you would print out an error message and do a continue statement for the loop to start over and ask the user for a command again.

    In addition, you'll need a new command, for example "Exii" that would let the user exit the program.

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Java User-Input bug (not continuing); NOVICE

    just so you know, your program only offer 2 variants of each attribute

    north or North, i ran your game and put NORTH it wouldnt work.

    equalsIgnoreCase(north);

    add this to each option instead, this will accepect any form of the word north, North, NORTH NoRtH ETC.

  6. #6
    Shzylo is offline Member
    Join Date
    Dec 2012
    Location
    Oklahoma, USA
    Posts
    5
    Rep Power
    0

    Default Re: Java User-Input bug (not continuing); NOVICE

    Quote Originally Posted by monkeyjr97 View Post
    just so you know, your program only offer 2 variants of each attribute

    north or North, i ran your game and put NORTH it wouldnt work.

    equalsIgnoreCase(north);

    add this to each option instead, this will accepect any form of the word north, North, NORTH NoRtH ETC.
    Thank you, I knew there was a simpler method :)

    kaydell, I will add an exit option, but this is just phase 1. I hope to carry this text adventure a while, it will take up back to the old days :)

  7. #7
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: Java User-Input bug (not continuing); NOVICE

    just make sure if you do a do-while loop you do infact make a valid way to exit, otherwise you will have an infinate loop :L

    pretty obvious statement but just making sure you remember!

  8. #8
    Shzylo is offline Member
    Join Date
    Dec 2012
    Location
    Oklahoma, USA
    Posts
    5
    Rep Power
    0

    Default Re: Java User-Input bug (not continuing); NOVICE

    ok, I got that going. I just added a do-while loop like this:
    Java Code:
    package main;
     
    public class Main extends Variables {
         
    	static int x = 0;
    	static int y = 0;
    	
        public static void start() {
            System.out.println("You wake up lost, washed ashore on a beach. All you see in sight is a twig, a rock, and lots of sand.");
            System.out.println("COMMANDS: go \n" + "OPTIONS: North, South, East, West");
            direction = i.nextLine();
            
            do {
            if(direction.equals("North")) {
                north();  
                 
            } else if(direction.equals("South")) {
                south();
                
            } else if(direction.equals("East")) {
                east();    
                 
            } else if(direction.equals("West")) {
                west();
            } else { System.out.println("I do not recognize that command..");
            } while(x == y);
        }
         
        public static void north() {
            System.out.println("You have traveled north into a forest, where you see a turtle resting on a rock, nothing special in this direction.");
            sleep();
            System.out.println("Please choose an action:");
            direction = i.nextLine();
        }
        public static void south() {
            System.out.println("I wouldn't go that way, there is an aweful lot of water there.");
            sleep();
            System.out.println("Please choose an action:");
            direction = i.nextLine();
        }
        public static void east() {
            System.out.println("Oh, a snake, it's staring and hissing at you. I would get some defense before I go there.");
            sleep();
            System.out.println("Please choose an action:");
            direction = i.nextLine();
        }
        public static void west() {
            System.out.println("I see rope,  and some bass in a small pool of water");
            sleep();
            System.out.println("Please choose an action:");
            direction = i.nextLine();
        }
    }
    Last edited by Shzylo; 12-13-2012 at 06:31 AM.

  9. #9
    Shzylo is offline Member
    Join Date
    Dec 2012
    Location
    Oklahoma, USA
    Posts
    5
    Rep Power
    0

    Default Re: Java User-Input bug (not continuing); NOVICE

    I also need help with another bug :P

    If I need to post a new topic, ok.

    But if you want to know the bug it is this:

    USER-INPUT: no
    PRINTLN: I do not recognize that command..
    the PrintLn will loop infinitely.

Similar Threads

  1. Java Date input from User,
    By Shahnawaz in forum New To Java
    Replies: 22
    Last Post: 09-07-2011, 02:20 AM
  2. Replies: 5
    Last Post: 03-23-2011, 11:28 PM
  3. Replies: 13
    Last Post: 04-16-2010, 03:51 AM
  4. Replies: 6
    Last Post: 03-01-2010, 08:00 AM
  5. How do I validate user input from forms with Java?
    By rickywh in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 07:49 AM

Tags for this Thread

Posting Permissions

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