-1

So I am having an issue, I put my self to the test of creating a 2D sandbox game kind of for fun, but has turned out evolving into something a bit more after about a week, but, to the point, I am saving chunk data into a few files, these are just .file or, just the most basic file type I assume, and the problem persists even using .txt files. So what's happening is after saving a file containing the object name, x, and y positions, I am creating an object in a linked list using that information, when I enter a new "chunk", but things are not exactly right, somehow, my images load up for the tiles correctly, which is handled in the constructor for that tile, being called after it is added to the list, however, what made me notice is I have a check in the tiles update method, to see if it wants to change to a different grass texture, and it doesn't happen, so I did some checking and printing, and well, I can tell that the name variable certainly contains the desired name, eg. "dirt", however it does not equal "dirt", so I thought it was catching some spaces and I even trimmed the String, and when I run a test like

if(name == "dirt")System.out.print("dirt is actually "+name);
else System.out.print("dirt is apparently not "+name);

and the result is always, "dirt is apparently not dirt", with no spaces, and even when looking in the actual file I save it to, it saves the name fine, I have them saving in an order like so;

name x y
eg.

dirt 0 560
dirt 28 560

and so on, these all load in position perfectly fine and what stumps me is that the initial image loads in correctly, which is quite explicit, a switch statement to determine what the image, material type and such should be..

I was wondering if anyone has come across something like this and what it could possibly be.

Also any code desired I will add to the desc.

(I didn't know so many people would be so quick to help. Thank you all, I know it was a simple problem, but it had me super confused, now thanks to you all I understand something about comparing variables and objects I should have really known before, and which is really important .)

  • Try this code: if(name.intern() == "dirt") System.out.print("dirt is actually "+name); or if("dirt".equals(name)) System.out.print("dirt is actually "+name); Btw, how do you create the variable name? – Octavian R. Mar 15 '19 at 21:19
  • Well the .equals is working. I create the name through a constructor condition. – 4pricot_3ae Mar 15 '19 at 21:29

2 Answers2

0

Chances are the result is "dirt" but you're not testing it correctly.

Test should be if name.equals("dirt") ... not the equality operator ==.

See: How do I compare strings in Java?

stridecolossus
  • 1,449
  • 10
  • 24
0

Equality in Java:

String dirt1 = "dirt";
String dirt2 = new String("dirt");

System.out.println(dirt1 == "dirt"); // TRUE
System.out.println(dirt1 == dirt2);  // FALSE
System.out.println(dirt1 == dirt2.intern());  // TRUE
System.out.println(dirt1.equals(dirt2));  // TRUE
Octavian R.
  • 1,231
  • 8
  • 12