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 10 of 10

Thread: Arrow keys to navigate a maze in a console program

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Arrow keys to navigate a maze in a console program

    I'm very new to Java, and I have checked all over the internet and haven't been able to find an easy, suitable answer to my needs.

    I'm making a small maze program, and would like to print out an image of the maze, ask the user to press the arrow key in the direction they would like to go, and execute that command. The program would have to stop and wait for an arrow key input.

    Do I have to make a new method for this, or can it be done in the main method?


  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    11
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Arrow keys to navigate a maze in a console program

    The scanner object waits for input. It may be a better idea to have it in its own method so the method can prompt for the direction, check if its valid, check to see if the end of the maze, and it can just recall itself at the end if it needs to keep going.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arrow keys to navigate a maze in a console program

    Sorry, but how would I do that? I want to take input from the arrow keys. I can make it so that you type "u", "d", "l", and "r" followed by enter to navigate the maze.

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Arrow keys to navigate a maze in a console program

    Check out the Scanner class; it is a way to get input. After you get input, you can just check to see if it matches the values "u", "d", "l", or "r" by using the <String>.equals() method. As marylandfour said, you should also make sure that each time input is taken, a check is made to see if the user has reached the end of the maze, and if not, the method should call itself.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrow keys to navigate a maze in a console program

    I don' think that you can use the arrow keys in console input.

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arrow keys to navigate a maze in a console program

    Snowguy:

    The program is completely written. I use the scanner and .equals to do all the checks and everything. I want to know if there is a way to instead use the arrow keys.

    I found something called KeyEvent and getKeyCode(), but no matter what I do, I can't get it to work.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrow keys to navigate a maze in a console program

    Those classes are for GUI apps.

  8. #8
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arrow keys to navigate a maze in a console program

    Ah. Thanks. Then is there any way to use a scanner to read an input immediately after it is typed, instead of waiting for the press of "ENTER"?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrow keys to navigate a maze in a console program

    I don't think so. Write a small test program and see what happens.

    You may need to start learning GUI so you can do these kinds of things.

  10. #10
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Arrow keys to navigate a maze in a console program

    Ok. Thanks. GUI is next in my learnings.

Similar Threads

  1. Moving an image around the screen using the arrow keys.
    By nemo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2013, 12:08 AM
  2. problem with a maze program
    By skoon in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 23rd, 2012, 07:04 AM
  3. How do you make a console program a .jar in Eclipse?
    By Programmer142 in forum Java Theory & Questions
    Replies: 9
    Last Post: January 13th, 2012, 12:13 AM
  4. need help creating a maze program
    By helpzor in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 2nd, 2011, 03:12 PM
  5. simple java console program, need help recalling commands
    By zero0000000 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 22nd, 2010, 05:47 AM