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: Living Without cin... or Pointers (A Thread About Java Text Adventures!)

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Living Without cin... or Pointers (A Thread About Java Text Adventures!)

    Hello all!

    Okay, so I guess it's pretty obvious from the title that I started programming in Java after I learned a bit of C++. I swapped because I basically found out that the fun little game project I was working on could never be run from a web page in a million years and no one on the internet trusts downloadable .exe files.

    Live and learn I guess.

    So anyway I've been trying to re-write the text adventure I was working on to teach myself C++, salvaging code here and there where I can, but I've run into some major snags.

    The first snag I ran into was that unlike in C++, where for a user to modify a variable you just need to do a little "cin <<" statement, in java you have to start off with a statement of "import java.util.Scanner;" before your class declaration to import that from a library of commands in the IDE (presumably), assign memory for the object in the main method like so:

    Scanner sc = new Scanner(System.in);

    (I really wish I knew what I was doing when I typed that)

    Then for the player to actually modify something, after I declare the variable, I need to type:

    String name = sc.nextLine();

    (I also really wish I knew what I was doing when I typed that)

    So I've basically figured out that detail, but it's just something I bumped into that wasn't very well explained rather than an object that I'm really familiar with.

    My second problem is that I'm trying to figure out how to tie those user modified variables to variables within a method so that I can make my status screen, here's a simplified version of that:

    public class Chapter1 {
     public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Please enter your character's name: ");
            String name = sc.nextLine();
            int raceChoice = 0;
            System.out.println("Choose your race: ");
            raceChoice = sc.nextInt();
        int playerHealth = 1;
        int roomTracker = 1;
     
        }
     
        public void breatheDeep(int raceChoice, int playerHealth, int roomTracker, String name) {
        System.out.print("You take a deep breath, close your eyes and look inward. You have a\nbrief moment of deep awareness. \n\n HEALTH: ");
    			System.out.print(playerHealth);
    			System.out.print("NAME: " + name+ " \n\n");
    			if (raceChoice == 1)
    			{
                                System.out.print("..................~()~()./|.....\n"
                                				+"..................(~0000/ /............\n"
                                				+"..............(~0/    _    \\............\n"
                                				+".............(~0/     Q     \\__.....\n"
                                				+"............(~0/             _D\\........\n"
                                				+"...........(~0/             ___/.........\n"
                                				+"..........(~0/          \\__/...................\n"
                                				+"____________/           /.........................\n"
                                				+"                       |..........................\n"
                                				+"                       |...........................\n"
                                				+"                       |..........................\n "                    
                                				+"                       |..........................\n"
                                				+"                       |.........................\n"
                                				+"                        \\.......................\n"
                                				+"                         \\.......................\n"
                                				+"________|     |_____      \\.......................\n"
                                				+"........|     |......\\     \\......................\n"
                                				+"........|     |.......\\     \\ .........................\n");
    			}
    			else if (raceChoice == 2)
    			{
                                System.out.print("..................~()~()......\n"
                                				+"..................(~000000............\n"
                                				+"..............(~0/    _    \\............\n"
                                				+".............(~0/     Q     \\__.....\n"
                                				+"............(~0/             _D\\........\n"
                                				+"...........(~0/             ___/.........\n"
                                				+"..........(~0/          \\__/...................\n"
                                				+"____________/           /.........................\n"
                                				+"|||||||||>>             |..........................\n"
                                				+"|||||||||>>             |...........................\n"
                                				+"|||||||||>              |..........................\n"                    
                                				+"|||||||||               |..........................\n"
                                				+"|||||||||               |.........................\n"
                                				+"|||||||||               \\.......................\n"
                                				+"|||||||||                \\.......................\n"
                                				+"|||||||||     |_____      \\.......................\n"
                                				+"--------|     |......\\     \\......................\n"
                                				+"........|     |.......\\     \\ .........................\n");
    			}
    			else if (raceChoice == 3)
    			{
                                System.out.print("..................~()~()......\n"
                                		        +"..................(~000000............\n"
                                				+"..............(~0/    _    \\............\n"
                                				+".............(~0/     Q     \\__.....\n"
                                				+"............(~0/             _D\\........\n"
                                				+"...........(~0/             ___/.........\n"
                                				+"..........(~0/          \\__/...................\n"
                                				+"____________/           /.........................\n"
                                				+"                       |..........................\n"
                                				+"                       |...........................\n"
                                				+"                       |..........................\n"                   
                                				+"                       |..........................\n"
                                				+"                       |.........................\n"
                                				+"                        \\.......................\n"
                                				+"                         \\.......................\n"
                                				+"        |     |_____      \\.......................\n"
                                				+"--------|     |......\\     \\......................\n"
                                				+"........|     |.......\\     \\ .........................\n");
                 }
    				//I'll need to fix the ascii art above, it's a great idea, but I have to figure out how to get the spacing right and make the \ character show up.
    			if (roomTracker == 1)
    			{
    	System.out.print("CLINIC\n");
    	System.out.print("00000000_____000000000000000000000000000000000\n");
    	System.out.print("0       DOOR            U()U       |         0\n");
    	System.out.print("0                      WASH BASIN  |         0\n");
    	System.out.print("0                                  |CABINET  0\n");
    	System.out.print("0                                  |         0\n");
    	System.out.print("0                                  ----------0\n");
    	System.out.print("0                                            0\n");
    	System.out.print("0                                            0\n");
    	System.out.print("0                                            0\n");
    	System.out.print("0                                            0\n");
    	System.out.print("0-------        --------                     0\n");
    	System.out.print("0      |        |      |                     |\n");
    	System.out.print("0 YOUR | (MARE) |      |                 DOOR|\n");
    	System.out.print("0 COT  |        | COT  |                     |\n");
    	System.out.print("0      |        |      |                     0\n");
    	System.out.print("0      |        |      |                     0\n");
    	System.out.print("0000000000000000000000000000000000000000000000\n");
                                System.out.print("You are in some sort of clinic. It is tidy and neat, but doesn't\nappear to have much in the way of medical supplies. Everything is worn\ndown, but obvious efforts have been made to maintain what little is\nthere. A mare in an orange vest resides here, looking watchful. There\nis also a heavy metal cabinet tucked away in a corner. It has a very\nsturdy chain lock.\n\n");
     
    			}    
        }
        }

    See in C++ I could just use pointers to tie how the variables were modified by the user input to "universal variables" that are read as the same value no matter where they are referred to in the code, but that doesn't appear to be the case in java (I also don't know how to call that method easily, but that's another story).

    I've been trying to teach myself how to program in java using the Sun tutorials here: The Java™ Tutorials
    But honestly they are deeply confusing and don't follow a very logical order so it's been difficult for me to figure out answers to fundamental questions like this. I've gone to a huge pile of sites trying to get straight answers, but they're all really difficult to sort through.

    I know these are all really silly newbie questions, but I'm really, truly lost at this point. Can any of you intelligent and experienced programmers help me out? I would be deeply grateful for any experience you could provide.


  2. #2
    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: Living Without cin... or Pointers (A Thread About Java Text Adventures!)

    Java doesn't have the pre-processing that C++ has to allow syntax like: cin <<
    Everything must be fully and explicitly coded. Not too many things are hidden by the compiler(except for boxing)
    You can think of all object reference variables as pointers. All variables are passed to methods by value.

    how to tie those user modified variables to variables within a method
    Pass them as arguments to a method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Living Without cin... or Pointers (A Thread About Java Text Adventures!)

    Java was designed to be used in an Object-Oriented manner as much as possible. This means things such as "globals" don't exist. There are ways to simulate globals, but you should really avoid using these whenever possible.

    Instead, break your data up into appropriate classes and objects. For example, you have data related to the player. This would be a good place to create a Player class which holds the player's name, race, and other data. The Player class could also hold methods for manipulating the Player object such as accessing the player data and making modifications to the Player object. There should also be constructor methods in the Player class which will build a functioning Player object

    Once you have defined a Player class you can create a Player object in a similar manner to how you created the Scanner object.

    Here's a simple example:

    Player class:
    public class Player
    {
        private String name;
     
        public Player(String n)
        {
            name = n;
        }
     
        public void setName(String n)
        {
            name = n;
        }
     
        public String getName()
        {
            return name;
        }
    }

    In your main class:

    public static void main(String[] args)
    {
        // asks the player for their name, then greets them.
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter player 1's name: ");
        Player p1 = new Player(sc.nextLine());
     
        System.out.println("Welcome, " + p1.getName());
    }

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Living Without cin... or Pointers (A Thread About Java Text Adventures!)

    Quote Originally Posted by helloworld922 View Post
    Java was designed to be used in an Object-Oriented manner as much as possible. This means things such as "globals" don't exist. There are ways to simulate globals, but you should really avoid using these whenever possible.

    Instead, break your data up into appropriate classes and objects. For example, you have data related to the player. This would be a good place to create a Player class which holds the player's name, race, and other data. The Player class could also hold methods for manipulating the Player object such as accessing the player data and making modifications to the Player object. There should also be constructor methods in the Player class which will build a functioning Player object

    Once you have defined a Player class you can create a Player object in a similar manner to how you created the Scanner object.

    Here's a simple example:

    Player class:
    public class Player
    {
        private String name;
     
        public Player(String n)
        {
            name = n;
        }
     
        public void setName(String n)
        {
            name = n;
        }
     
        public String getName()
        {
            return name;
        }
    }

    In your main class:

    public static void main(String[] args)
    {
        // asks the player for their name, then greets them.
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter player 1's name: ");
        Player p1 = new Player(sc.nextLine());
     
        System.out.println("Welcome, " + p1.getName());
    }
    Okay! I was wondering what all of the really short methods I was seeing that defined a single variable were about. I have a question though, Would any of the methods you listed work? Are you calling just one of them or a class? Seriously, I've been looking through the tutorials for ages and I haven't been able to figure out the syntax for how you call a method from an outside class.

    Thank you for the help!

  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: Living Without cin... or Pointers (A Thread About Java Text Adventures!)

    the syntax for how you call a method from an outside class.
    An example:
    p1.getName());
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Living Without cin... or Pointers (A Thread About Java Text Adventures!)

    Quote Originally Posted by Norm View Post
    An example:
    p1.getName());
    I'm very sorry to bother you about this, but would you be able to break that down for me? I get that the "pl" part is the instance of the class player within the main method, and that the code you wrote will output the player name, but I'm not sure if the anatomy of what helloworld992 told me quite makes sense in that context:

    Player p1 = new Player(sc.nextLine());

    I get that "new" is the keyword that assigns memory to an object and that what's within the function "Player" that he called is a method called from the Scanner class instance "sc", but would you be kind enough to explain to me how that all fits together?

  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: Living Without cin... or Pointers (A Thread About Java Text Adventures!)

    Player p1 = new Player(sc.nextLine());
    Player is a class
    p1 is defined as a variable of type Player
    new creates an instance of the class: Player
    = assigns the pointer to the class returned by new to the variable: p1
    sc is a variable that is reference/pointer to a class object
    nextLine() is a method in that class that returns a value passed to the Player class's constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Living Without cin... or Pointers (A Thread About Java Text Adventures!)

    Sorry, I kind of combined several operations into one line.

    The code can be expanded into several lines for a clearer picture:

    // declare 3 variables: sc, player_name, and p1
    // note: the variables have not been initialized yet (given a value)!
    Scanner sc;
    String player_name;
    Player p1;
     
    System.out.print("Enter player 1's name: ");
     
    // calls the nextLine() method of the Scanner object sc
    // stores the result into the variable player_name
    player_name = sc.nextLine();
     
    // create a player object, the constructor takes 1 string parameter (the player's name)
    p1 = new Player(player_name);

    When you call sc.nextLine() creates a String and returns where this string is being stored. The actual string is independent from any variable, which is a convenient way to find where the object is stored in memory (you can think of Java object variables as quasi-pointers).

    In this verbose example I'm storing a reference to the returned string in the variable player_name. However, this isn't necessary as I can just pass this "address" to the Player constructor (which is what I did above).

    p1 = new Player(sc.nextLine());

  9. #9
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Living Without cin... or Pointers (A Thread About Java Text Adventures!)

    Quote Originally Posted by helloworld922 View Post
    Sorry, I kind of combined several operations into one line.

    The code can be expanded into several lines for a clearer picture:

    // declare 3 variables: sc, player_name, and p1
    // note: the variables have not been initialized yet (given a value)!
    Scanner sc;
    String player_name;
    Player p1;
     
    System.out.print("Enter player 1's name: ");
     
    // calls the nextLine() method of the Scanner object sc
    // stores the result into the variable player_name
    player_name = sc.nextLine();
     
    // create a player object, the constructor takes 1 string parameter (the player's name)
    p1 = new Player(player_name);

    When you call sc.nextLine() creates a String and returns where this string is being stored. The actual string is independent from any variable, which is a convenient way to find where the object is stored in memory (you can think of Java object variables as quasi-pointers).

    In this verbose example I'm storing a reference to the returned string in the variable player_name. However, this isn't necessary as I can just pass this "address" to the Player constructor (which is what I did above).

    p1 = new Player(sc.nextLine());
    I'm still a little confused here, how did the code know that the constructor was the method from the Player class that you were calling? You declared three.

    Also, (and here's the part where I reveal just how new to this I am) what does passing an argument mean?

    Thank you for the continuing help by the way.

  10. #10
    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: Living Without cin... or Pointers (A Thread About Java Text Adventures!)

    how did the code know that the constructor was the method from the Player class
    By looking at the data type returned by the nextLine() method and using the Player class constructor with that data type. Each constructor must have a unique set of arguments.

    There is some confusion here, the Player class in post#3 only has one constructor.

    Take a look at: http://docs.oracle.com/javase/tutori...aOO/index.html
    Last edited by Norm; September 17th, 2012 at 07:57 PM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Making a living from Java in strange ways :)
    By Squiffy in forum Member Introductions
    Replies: 3
    Last Post: November 21st, 2011, 05:10 AM
  2. Replies: 0
    Last Post: October 29th, 2011, 02:37 AM
  3. Have a C++ project due soon and am not sure if I'm using pointers right.
    By javapenguin in forum Other Programming Languages
    Replies: 12
    Last Post: September 19th, 2011, 09:54 PM
  4. Null pointers
    By Jared in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 7th, 2010, 06:40 PM
  5. [SOLVED] Problem in generating Fibonacci sequence in java
    By big_c in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 24th, 2009, 08:52 AM