Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

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

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Location
    Okahoma, US
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up 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
    package main;
     
    import java.util.Scanner;
     
    public class Game extends Variables {
     
    	public Game() {
    	}
    	public static void main(String[] args) {
    		Main.start();
    	}
    }

    Variables.java
    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
    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();
    	}
    }
    Last edited by Shzylo; December 11th, 2012 at 09:56 PM. Reason: Moderator told me to change format.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

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

    Suggestions to help us help you:

    1. Get rid of the outer [quote] [/quote] tags in your post above as they're distracting.
    2. Change the inner [quote] [/quote] tags into [code] [/code] tags so that your posted code will show its formatting.
    3. If your code is generating an error or exception, then post the full message here, and indicate in some obvious way which line(s) is causing the error.
    4. As always, check the forum FAQ for details on how to use code tags and how to get the best experience here.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Location
    Okahoma, US
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Yes, sir. There is not error file generating, it just terminates.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

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

    Add some println statements to show where the code is executing to find out the last place it executes before it terminates.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Location
    Okahoma, US
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    I have fixed the bug by putting the IF_ELSE statement inside of a do-loop.
    I have another bug that will consist of a loop, but I don't know how to do the loop correctly, or what loop

    It will continuously print out "I don't know that command" if I don't put in the correct command.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

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

    Same advice as before.
    Also you need to post the new code.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Location
    Okahoma, US
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    All I did was edit the main class, so here it is:

    Main.java
    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 stick, a rock, and lots of sand.");
            System.out.println("Where would you like to go? North, South, East, West");
            direction = i.nextLine();
     
            do {
            if(direction.equalsIgnoreCase("north")) {
                north();  
     
            } else if(direction.equalsIgnoreCase("south")) {
                south();
     
            } else if(direction.equalsIgnoreCase("east")) {
                east();    
     
            } else if(direction.equalsIgnoreCase("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();
        }
    }

    bug of looping image:

    hTTPy.jpg

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

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

    What was the value of the variable: direction that was not recognized?
    The error message should print out the invalid value.

    What are the values of x and y while the loop is executing? Because they control the looping you should print them out as the loop goes around to see why they don't change to allow the loop to exit.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Location
    Okahoma, US
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    What was the value of the variable: direction that was not recognized?
    The direction not recognized is everything besides "north, south, east, west" capitalization not affected.

    The error message should print out the invalid value.
    The invalid value i always type is 'no' to test loop.

    What are the values of x and y while the loop is executing?
    int x = 0;
    int y = 0;

    no ++ for the variable, or at least yet.


    Here i did System.out.println(x); and System.out.println(y);
    Capture.JPG

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

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

    Please copy and paste the programs print outs here instead of using images. The images don't show all that was printed.


    What was the value of direction? It looks like 2 dots: ..
    Is that what was typed in? The contents of the command prompt console should show what was typed in to the program.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Dec 2012
    Location
    Okahoma, US
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    The dots is part of the Println method. The value of direction is n/a
    public static String direction = "";

    It would help if you look at my code: Shzylo's Text Adventure.ZIP

    don't type 'help' though..

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,213 Times in 2,186 Posts

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

    The value of direction is n/a
    That does not make sense for the value of a String.
    Include the value of direction with the error message so you can see what the computer sees when the if statements are executed.
    Then execute the program and copy the contents of the console here with the print out that shows the value of the direction variable.

    Post the code here on the forum. I don't go to links or zip files.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Take user input
    By frabbi.cse in forum Java Theory & Questions
    Replies: 1
    Last Post: July 22nd, 2012, 12:48 PM
  2. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  3. Java Bar graph takes user input to create graph?
    By kenjiro310 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 07:37 AM
  4. Die Rolling - Continuing the statement?
    By Override in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 29th, 2010, 01:21 AM
  5. Novice with java methods, please help!
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2009, 04:23 AM

Tags for this Thread