aspose file tools*
The moose likes Beginning Java and the fly likes Help with a get method that returns positions of a variable. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with a get method that returns positions of a variable." Watch "Help with a get method that returns positions of a variable." New topic
Author

Help with a get method that returns positions of a variable.

Deena Lee
Greenhorn

Joined: Aug 19, 2010
Posts: 13
I have an entire program wrote, but I'm having problems with the getMethod. I need a get method which will receive the position of the variable (1,2, or 3) and will then return the variable at that position. Here's an example of the call to the method: System.out.println(dog.getFido(2));

I appreciate any help you can give me.

Thanks!
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
Well let's start with what kind of problem are you having? Compiler error? Runtime error? The code doesn't produce what you expected it to do? It would also help if, along with specifying your problem, you post your code. Before posting your code, please read the UseCodeTags page.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Position in what?
Deena Lee
Greenhorn

Joined: Aug 19, 2010
Posts: 13
At the moment I'm not getting compiler errors, but it's not returning the correct values. I don't want to post all my code, because the last time i did that another student in the class picked my code up and tried to have the prof. help her with my problem. Needless to say he accused me of cheating!

So here is my getMethod, as of now,

[public int getFido(int index)
{
return Fido;
}]

The problem I'm having is that there are 3 different "fido's", OneFido, TwoFido and ThreeFido, each already set with a different name. How do I get all of them set to the getMethod so that I can print them by calling dog.Fido(1), dog.Fido(2), or dog.Fido(3)?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Again--position in what? Does each dog know its position? How are the doggies stored?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60059
    
  65

Please read TellTheDetails


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Deena Lee
Greenhorn

Joined: Aug 19, 2010
Posts: 13
No the dogs don't know their positions at all. That is what I'm not understanding how to do. The book, nor the lectures, talked about this.
Thanks!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

...

In order to know how to return their position, we need to know their position in what. Are they in an array? A list? Is the only indication of their position the name of their references (in which case, can't be done)? You have to understand that we don't know your assignment, we don't have your instructor in front of us to ask, we don't know the rest of the code--we simply don't have enough information we can use to help.
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

If you could not give enough information, it is hard for us to help.
Did you try to get help from your professor?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Deena --

There are are two alternatives, I think -- the right way, and the wrong way.

The right way is to use an array or a java.util.ArrayList. Then "position" translates directly into "position in the array or ArrayList". Have you learned about these things in class?

The wrong way is to use a bunch of "if" statements:



[Jess in Action][AskingGoodQuestions]
Deena Lee
Greenhorn

Joined: Aug 19, 2010
Posts: 13

I know about arrays, but haven't gotten to that chapter yet. Plus, the instructor told me I can't use them. The instructor isn't much help. His lectures are posted straight from the textbook.

So anyway, I tried the If else statements ... I really liked the idea . However, for all the print statements it returned the name of fido(3) (The print statements were provided in the instructions)

Here is the code I tried:

[public int getFido(int index)

{
if (fido == 1)
return oneFido;
else if (fido == 2)
return twoFido;
else
return thirdFido;
}]

Thanks so much!!
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

Deena Lee wrote:
Here is the code I tried:

If your index is 4, what would you get in this method ?

By the way, your indentation seems have mix up of spaces and tabs which you should avoid.
Deena Lee
Greenhorn

Joined: Aug 19, 2010
Posts: 13
Raymond Tong wrote:
Deena Lee wrote:
Here is the code I tried:

If your index is 4, what would you get in this method ?

By the way, your indentation seems have mix up of spaces and tabs which you should avoid.


Sorry, but I don't understand your comment about index 4. I only have 3 dogs. The print statements, I have to use, to get their names are ...
System.out.println(dog.getFido(1))
System.out.println(dog.getFido(2))
System.out.println(dog.getFido(3))
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

Deena Lee wrote:
Sorry, but I don't understand your comment about index 4. I only have 3 dogs. The print statements, I have to use, to get their names are ...
System.out.println(dog.getFido(1))
System.out.println(dog.getFido(2))
System.out.println(dog.getFido(3))

What I am saying is, if you do

What do you expect? Or this statement would not happen anyway ?
Deena Lee
Greenhorn

Joined: Aug 19, 2010
Posts: 13
Raymond Tong wrote:
Deena Lee wrote:
Sorry, but I don't understand your comment about index 4. I only have 3 dogs. The print statements, I have to use, to get their names are ...
System.out.println(dog.getFido(1))
System.out.println(dog.getFido(2))
System.out.println(dog.getFido(3))

What I am saying is, if you do

What do you expect? Or this statement would not happen anyway ?


Ah I see ... Well I wasn't going to concern myself with a fido(4), because the print statements are hard coded and there isn't one for fido(4). But, I guess it doesn't matter if I can't figure out how to set each fido(#) to it's its respective variable that contains it's its name.

Although if I was getting user input I'd have to make sure that fido(4) returned an error statement or a null statement.

Edited for proper use of "its".
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Deena Lee wrote:
[public int getFido(int index)

{
if (fido == 1)
return oneFido;
else if (fido == 2)
return twoFido;
else
return thirdFido;
}]


The method doesnt seem to be using the 'index' thats passed to the method. Look at EFH's post (the wrong way part of it).
Deena Lee
Greenhorn

Joined: Aug 19, 2010
Posts: 13
Ahhhh .... DUH! ... what a blonde moment. Live and learn ... and I learned a lot from this one.

Thank you, thank you ... to everyone who has helped me!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Deena Lee wrote:Ahhhh .... DUH! ... what a . . .
Are you a blonde? Don't worry: everybody does that sort of thing. And not only when they are beginners. And welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with a get method that returns positions of a variable.
 
Similar Threads
Forward Referencing
JTable Problem
HELP! Connection to MS Access DB Trouble
add text into JTextPane through JButton, but not replace other text
getter not returning subclass instance variable values set by setter