Okay, so i need help getting a value from a variable in one class's method. I want to use that data in another Class's method. Heres what i mean...

public class RoomType{
        public String N(String[] args){
            Scanner scan = new Scanner(System.in);    
            String opt = "";
            int x=1;
            do{
            opt = scan.nextLine();
            
            if(opt.equalsIgnoreCase("n")){
                x=2;
            }
            else{
                System.out.println("Please select a valid direction");
            }
            }while(x==1);
        return (opt);
        }

and i want the string "opt" to come up in my "Map" Class in one of the methods.

any help would be greatly appreciated.
thanks
Jesse

There are a couple of things you could mean here.

If you want, you could call this method from Map, and assign the return value to some locally available String.

someString = RoomType.n(someStringArray);

On the other hand, you might mean that you want to run this once and refer to the contents of opt repeatedly. In that case, you would do it a little differently - in a nutshell, you'd want to store opt in a class field and provide a method for getting at it, a "getter" method.

Or you might mean something else, but let's start there.

Now, I think you might be a little confused about the parameter for N - it's not used in the method, and it looks like you copied it from the declaration of the main() method. You need that in Main, so the jvm will recognize the main method, and know where to start. You don't need it in every method. Get rid of it (and alter the function call I gave you accordingly)

Well i would like to do the second one. Run N once then use the data from opt repeatedly, and sorry but I'm REALLY new to java so I'm not sure how to store opt in a class field or make a "getter method".

I'm only 15 just starting to learn java if that will help excuse my ignorance.

thanks
Jesse

Ignorance is a temporary condition, given curiosity, and it sounds like you've got that.

Okay, here's a quick rundown on what you need to do, but you'll probably want to do some research on this. A few hours with a search engine and some experimentation should straighten you out.

A class field is a top-level variable, belonging to an object (or to the class itself, if it's labelled static - push "static fields" onto your stack of things to learn about later). A field is different from a method variable in that it is accessible anywhere in the object that it belongs to, while a method variable only exists for the life of one call to that method. Class fields, therefore, are persistent in a way that method variables are not.

Here's some toy code that shows some of this:

class Foo
{
private int fooField = 0;

public void bar()
 {
  int barVariable =3;

  System.out.println(fooField); //this works, bar can see foo's fields
                                // prints 0
  fooField = barVariable; // fine, now fooField = 3
 }

public void fribble()
 {
  System.out.println(barVariable); // FAILS - barVariable doesn't exist now!

  bar();  // call the method
  System.out.println(fooField); // what do you think will be printed? 
 }
}

All of this has to do with where the stuff is stored, and you can learn about that later. You'll hear about heaps and stacks and stuff - again, save that for later. But if you want the data that you get from your user when you call N() to be available after N has done its thing, you have to assign it into a field of some class, somewhere.

To make this happen, you declare a String outside of any of the methods of the class, as I did with fooField. Make it private - you will almost never declare your data as public, for reasons, again, which you'll get to later. Now, when you call your method N, it will return a String value. All you need to do is to store that value into that field, and it's available to you as long as the program is running.

In order to get that data later, you want to put it into an "accessor", commonly called a "getter" method. Typical form of this is

public String getMyString()
{
return myString;
}

Then, you can call this from somewhere else like so:

mapString = foo.getMyString()

Pretty simple, no?

Well, no, it's not very simple. This is a highly condensed treatment of how this works, but it'll help you get started anyway.
One of the problems with learning Java on your own is that there's a pretty high threshold of understanding before the pieces start to fall into place, but the only way to get there is to struggle through and ask questions, as you're doing, and to read a lot of explanations of the same ideas. Sometimes you have to take all the ideas and just go over them repeatedly until they click together.

THANKS! that helped a ton! I'm experimenting with a Text based game if you know what im talking about. sort of like "Swords of chaos". Could you take a look at my code sometime and tell me what you think?

thanks
Jesse

Don't know Swords of Chaos. When you say "text-based", do you mean something like Zork, or the other old infocom games? Those are pretty good stuff. You know, Infocom had a pretty cool engine for making those games work. Sort of an early virtual machine, really. If you poke around, you can actually find implementations of their engine - I think they called it the Z-Machine, or something like that. That might give you some ideas on how this sort of thing has been done once.

I've never heard of those games either. but when i say text base i mean like...

"You enter a dark dungeon, obvious exits are North and South. n/s?"

and you go through a dungeon. Eventually i hope to add leveling up your character, fighting monsters and items. but thats a bit much for now. im just working on getting a dungeon where you can go from room to room.

This may distract you a little from your coding, but go here and download Zork, plus an appropriate Z machine for your system. You might have to try a few, the interpeters are all pretty much amateur affairs. Once you find one that works, the games should play fine. And there's a ton of games, so I'm afraid I may have just ruined your social life for the next few years... sorry about that.

Some of the later infocom games have a clever form of copy-protection: important information is embedded in the packaging. Fortunately, scans of the packaging for all of the games are available at the infocom gallery - link from illuminion. Zork does not have this sort of copy protection, everything you need is in the game itself.

Okay, thanks. I just finished a Beta version of the walk around maze. It has 11 rooms and seems to be working fine :) and ill definitely check out Zork

Okay, thanks. I just finished a Beta version of the walk around maze. It has 11 rooms and seems to be working fine :)

Cool. Isn't it great when you're trying to do something - and it works? The best thing about programming is, that "hey, it worked" moment doesn't go away. Even when you're old and jaded, if you're trying new stuff, you get that.

So how are you setting up your game? Give me the no-code overview. Since you're using Java, I imagine there's a class that represents rooms, and each room instantiates it - is that right? What are you doing to keep track of the player as they move through the maze?

yeah. i love that feeling, especially when i first started with VB :P

Im using a class to keep track of the rooms, and its got a method for each room. I have a map class that has all comments just so i can see what the map looks like(I drew a map on it with asterisks) The main class starts up running the starting room method and it all kind of branches off from there...its not great and i dont have a lot to keep track of the player yet.

If you would like i could send you over the source files and you could tell me what you think? (I'm using eclipse if it makes a difference)

Member Avatar for coil

With this sort of project planning is really important, otherwise you get lost pretty quickly.

It seems like you have some sort of plan...maybe use a variable to keep track of the current room that the player is in?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.