0

I have an IntPair class which has two methods I use from it: "getFirst()" and "getSecond()". While in this method I'm currently working, I want to check if "hashMap j" contains a specific value and then do the actions. I think I have the problem in these lines:

Object obj = j.values();
t.moveCursor(((IntPair)obj).getFirst(), ((IntPair)obj).getSecond());

I don't know if I'm casting ok to the Object, or if the first line "Object obj = j.values()" should be replaced with another method call. I tested with a System.out.print ("Message") after j.containsValue("0") and I got the message back.

This is a part of the method I'm trying to make it work.

public static HashMap<IntPair, String> j = new HashMap<>();

j.put(new IntPair(firstInt, secondInt), value);
if (j.containsValue("0"))
{
Object obj = j.values();
t.moveCursor(((IntPair)obj).getFirst(), ((IntPair)obj).getSecond());
t.putCharacter('x');
}
else if (j.containsValue("1"))
{
Object obj = j.values();
t.moveCursor(((IntPair)obj).getFirst(), ((IntPair)obj).getSecond());
t.putCharacter('v');
}

IntPair Class:

public class IntPair {
private final int first;
private final int second;

public IntPair(int first, int second) {
    this.first = first;
    this.second = second;
}

@Override
public int hashCode() {
    int hash = 3;
    hash = 89 * hash + this.first;
    hash = 89 * hash + this.second;
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final IntPair other = (IntPair) obj;
    if (this.first != other.first) {
        return false;
    }
    if (this.second != other.second) {
        return false;
    }
    return true;
}

public int getFirst() {
    return first;
}

public int getSecond() {
    return second;
}
}

Any help would be really appreciated. Thank you!

1
  • 1) I don't see a complete method where you state, "this is the method...". 2) I assume (and certainly hope) that your IntPair class overrides equals and hashCode() appropriately, correct? 3) I am not sure what exactly it is you're asking or that you're stuck on. You mention a question about casting -- is your attempt to cast causing an error or exception? Commented Jan 17, 2015 at 2:12

2 Answers 2

1

There is a big issue with the code you have written In line

t.moveCursor(((IntPair)obj).getFirst(), ((IntPair)obj).getSecond());

the expression

((IntPair)obj).getFirst() 

wont return the getFirst value as obj here is not of type IntPair but obj is a collection of elements IntPair returned by

Object obj = j.values();

so you have to retrieve IntPair element from this collection and then you can read getFirst() I have written a small program to make my point

public static HashMap<IntPair, String> j = new HashMap<IntPair, String>();

    public static void main(String[] args) {
        j.put(new IntPair(2, 3), "0");
        if (j.containsValue("0")) {
            Set<Entry<IntPair, String>> pairs = j.entrySet();
            Iterator<Entry<IntPair, String>> it = pairs.iterator();
            Entry e;
            while (it.hasNext()) {
                e = it.next();
                if (e.getValue().equals("0")) {
                    IntPair resultObj = (IntPair) e.getKey();
                }
            }

        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Note that the values() method returns a Collection of the values objects, here a Collection of Strings. You can't cast this to IntPair, and I'm surprised that your attempt is not causing a compiler error.

3 Comments

I edited the question. What should I use instead of values() ?
@XhensB: what are you trying to achieve? What error(s) are you seeing?
I'm not getting any error but the code is not working for me. I have a terminal screen and I want to take values from hashMap and than pass them as arguments at the method moveCursor(int x, int y). After I move the cursor to desired location, I want to fill it with characters. And the result is that I have a blank screen in the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.