0

I'm experiencing a really strange behaviour of my code. I'm trying to translate a specific column (in this case just a single row) dynamically through a button in java, but for some reason, I'm failing to do so.

String newWord;

System.out.println(table.getValueAt(0, 2));
if(table.getValueAt(0, 2)=="mashkull")
{
newWord= "male";
}
else if (table.getValueAt(0, 2)=="femer")
{
newWord= "female";
}
else
{
newWord= "other";
}
table.setValueAt(newWord, 0, 2);

I populated that specific cell (row = 0 , column = 2) with the string "mashkull" (which in my native language means male, not relevant though). When I click the button once, it prints on my console this:

mashkull

but for some reason, the if condition doesn't work properly with table.getValuAt method...yes I've tried casting it to (String) or using the String.valueOf method but it's still the same. And as you can see, it does print mashkull on the console, it just can't get verified by the if condition, but it goes to the else statement and runs:

newWord= "other";

And the next thing I see on the JTable I created is the word "other" instead of the word "male" . If I click the button again (if I run the same code for the second time) it writes a new line on it and that is:

other

Any idea of where's the problem? Even though I am casting the value of that cell to string it still doesn't work, is there any detail I need to know about the method table.getValuAt(row, column) or about casting?

Albos Hajdari
  • 113
  • 1
  • 12
  • 1
    `table.getValueAt(0, 2)=="mashkull"` -> `table.getValueAt(0, 2).equals("mashkull")` –  May 30 '18 at 00:31
  • Thanks a lot, it's working. How didn't I think of that before? Well it's also really strange since if I String a = "mashkull"; and then ask if(a=="mashkull") it worked, but when I did String a = String.Valueof(table.getValueAt(0, 2)); or String a = (String) table.getValueAt(0, 2); it didn't work. – Albos Hajdari May 30 '18 at 11:57
  • See [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) –  May 30 '18 at 12:57

0 Answers0