-1

Okay, so I have this in my Test class

    public Test(Player p, int locX){
    this.p = p;
    this.locX = locX;
}

(and then I have the getters/setters in also)

And in my main class when someone types the command /test this is what happens:

    Player player = (Player) sender;
    int locX = 0;
    Test t = new Test(player, locX);
    t.setLocX(player.getLocation().getBlockX();
    t.setP(player);
    olist.put(player.getName(), t);

So this is just a test project I'm using to get a better understanding on how to get two values (that are different) from an object. So I want to make it so when a player types /check the player name (stored inside the object) and the location that is in the object are displayed to the player. So far I only have this

    for(String s : olist.keySet()){
        if(s == player.getName()){
            //Here I would like to display the 2 values to the player, but not
            //sure how I would separate the two values :\
        }
    }

Even though it doesn't make sense that I'm storing the player name inside the object when I have it inside the hashmap, I'm using this test project trying to learn how to get two different values from 1 object (in an actual project I would need to get an location and an int, but I'm starting off simple ;P). (Also this is using the Bukkit API but that shouldn't really effect anything...)

Ethan
  • 141
  • 1
  • 2
  • 9
  • Related: [How do I compare strings in Java?](http://stackoverflow.com/q/513832/1065197) – Luiggi Mendoza Aug 05 '13 at 14:15
  • 5
    If it's a map, call it a map, not a list (that makes things quite confusing). Map has a get method - use `Test t = map.get(player.getName());` instead of your loop. – assylias Aug 05 '13 at 14:16
  • 1
    Can you explain why you are initializing your Test object, then immediately setting locX with the value that is already in locX and setting p with the value that is already in p? – LJ2 Aug 05 '13 at 14:18
  • Can you be more specific what you actually want from us and what is the problem that you are facing.Read question twice but still can't figure out what is the issue and what are you doing – Algorithmist Aug 05 '13 at 14:22
  • @Algorithmist The object has two values stored in it, the player value and the int value, I'm trying to find how do I separate the two values inside the object and display both values to the player (separately). – Ethan Aug 05 '13 at 14:37
  • what do you mean by separating the two values? – Algorithmist Aug 05 '13 at 14:41
  • Create `olist` as `Map` instead. That way each key can return as much as it has getter methods. Including the name. – zapl Aug 05 '13 at 14:41
  • When you get t, you get the two values in t~~~ What do you mean by separating them? You can print them out using System.out.println() – charles_ma Aug 05 '13 at 14:46

3 Answers3

0

Use the public getters for the values you are interested in. You say you have getters, well use them... (I might have misunderstood the question)

Also, I find it strange and unnecessary to use setters setLocX and setP just after creating the object t. I would set those when instantiating the object, through the constructor.

wojjas
  • 1,046
  • 1
  • 9
  • 21
0

You can just use your getters from the "t" object.

First, get the player from the "t" object:

Player p = t.getP();

Now that you have the player, get his name:

String PName = p.GetName();

You can take LocX from t:

int lx = t.getLocX();

you can print out like this:

System.out.println("Player: " + PName + " |LocX: " + lx);
Maviles
  • 3,209
  • 2
  • 25
  • 39
0

Warning: rather clumsy... bad practice, but since you are learning...

An alternative way of getting multiple value from a function when returning a object/collection is not an option, try modifying value of the object. eg

public class Number {
public int getValue() {
    return a;
}
public void setValue(int a) {
    this.a = a;
}
int a;}

public class BadPractice {
public static void main(String[] args) {
    Number i= new Number();
    Number j=new Number();
    Number sum=new Number();
    BadPractice b= new BadPractice();
    b.sum(sum,i,j);
    System.out.println("sum="+sum.getValue()+",i="+i.getValue()+",j="+j.getValue());
}
private void sum(Number sum,Number i, Number j) {
    // TODO Auto-generated method stub
    i.setA(10);
    j.setA(20);
    sum.setA(i.getValue()+j.getValue());    
}}

gives you result. sum=30,i=10,j=20

Prithvi
  • 11
  • 2