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

Thread: Help with simple text game...

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question Help with simple text game...

    Hello gang,

    I am looking for some direction - I can program but I am an industrial Engineer and so I program Electronics such as PICs and also lots of PLC Software... I have no Java experience but I used to be quite good with Fortran!

    My son has come home from school and he wants to build a decision based text game and so I have committed to assist him build it in Java >.< So I need some pointers - I will be using Netbeans and have a rough understanding of the bare bones of a simple Java program (Hello World!!)

    So here is my first question:

    I would like to print one of several responses onto the screen based on an answer that I give to a question ie.

    Question: You have entered a room, there are 3 doors please choose a door to enter: a) Door to the left, b) Door straight ahead, c) Door to the right

    I then input a, b or c and then some text will be printed to the screen.

    Please help - Ian


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help with simple text game...

    You're going to need a way of writing lines on the console, which comes for free with System.out - it's an object of type java.io.PrintStream, so you can println(String) to write some text onto the console followed by a newline. Next up, you'll need to read a line (the easiest way) of input from the user. That's not quite so easy, so open wide for some spoonfeeding: "new java.io.BufferedReader(new java.io.InputStreamReader(System.in)).readLine()"* will return a String object that will hold what the user entered, without the newline.

    Do you have access to the Java SE API documentation? There's a lot to read, but it's well worth reading. Start with the words in CamelCase above.
    Java Platform SE 6

    *I think the kids on the lawn outside may all be using Console.readLine() and Console.writer().println() nowadays.

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    iansmiler (May 24th, 2012)

  4. #3
    Junior Member
    Join Date
    May 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question Re: Help with simple text game...

    Okay - this is what I got to so far:

    package text.project;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    /**
     *
     * @author Ian Smith
     */
    public class TextProject {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            System.out.print("Enter your name and press Enter: ");
           BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
           String name = null;
           try {
             name = br.readLine();
           } catch (IOException e) {
             System.out.println("Error!");
             System.exit(1);
           }
           System.out.println("Your name is " + name);
        }
    }

    Now I want to put some if statements in - but I don't know how to go about it!!! Any help appreciated as always - Ian
    Last edited by iansmiler; May 24th, 2012 at 01:35 PM.

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with simple text game...

    Quote Originally Posted by iansmiler View Post
    Now I want to put some if statements in - but I don't know how to go about it!!! Any help appreciated as always - Ian
    Please use highlight tags when posting code.

    You're going to want to get into the habit of checking the basic tutorials before asking a question- it's faster for you, and it's easier to help you when you get stuck or have a specific question than it is to answer "how do I do this" type questions.

    Your new best friend, which is the first result for googling java tutorials: The Java™ Tutorials
    Of particular interest here, which is the first result for googling java if statements: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. The Following User Says Thank You to KevinWorkman For This Useful Post:

    iansmiler (May 24th, 2012)

  7. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Help with simple text game...

    Always use Exception.printStackTrace() in place of print("An error occurred!"). Java if statements look like
    if ("Ian".equals(name))
      System.out.println("Sifu");
    else
      System.out.println("Hello, " + name);

    Java has better help for this sort of thing than I can give you:
    Control Flow Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

  8. The Following User Says Thank You to Sean4u For This Useful Post:

    iansmiler (May 24th, 2012)

  9. #6
    Junior Member
    Join Date
    May 2012
    Posts
    12
    My Mood
    Confused
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Help with simple text game...

    Thanks guys - more exploration for me to do

Similar Threads

  1. Simple Text Quest Help
    By cshaffer123 in forum Java Theory & Questions
    Replies: 7
    Last Post: January 18th, 2012, 12:27 AM
  2. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM
  3. printing simple 3d text
    By cutie in forum Java Theory & Questions
    Replies: 3
    Last Post: September 1st, 2011, 07:31 AM
  4. Programming AI for simple game?
    By YouGoLast in forum Java Theory & Questions
    Replies: 2
    Last Post: May 28th, 2011, 08:53 AM
  5. Simple game in Java
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:04 AM