I need to keep a string from resetting each time the class is called, but haven't found any good way of doing it ...
Every time if (block != LastC){} evaluates to true. ( which shouldn't happen very often )

--- code snip ---
public class Player_Move implements Listener {
    String LastC; 	
	@EventHandler(priority = EventPriority.NORMAL) 
	public void onPlayerMove(PlayerMoveEvent event){
		String block = event.getPlayer().getLocation().getChunk().getX() + " " + event.getPlayer().getLocation().getChunk().getZ();
		if (block != LastC){
		    LastC = block;
			event.getPlayer().sendMessage(ChatColor.GOLD + LastC);
		}}}
--- code snip ---

Recommended Answers

All 8 Replies

You should use the equals() method for comparing Strings.

Answer the question please. The only boolean is using " != " ... I WANT the value of block passed to LastC so the if statement does not fire again unless different data is passed to the onPlayerMove method

Change the code to use the equals() method for comparing Strings. Do not use the != operator.

and don't be rude. if you keep comparing it with != it will compare whether or not those objects are linked to the same reference in your memory, not whether they have the same value.
just test this:

String aa = new String("a");
String ab = new String("a");

if (aa == ab)
	System.out.println("same a");
ab = aa;
if (aa == ab)
	System.out.println("same b");

I am a bit confused on how using " new " will improve the outcome.
Here is a dummy version of the original

public class Compare_Old_To_New {
	String Old_Data;
	public void  Am_I_Different(String New_Data){
		     // is the new data the same as the old data ?
		if ( Old_Data != New_Data){
			System.out.println("I am different");
			// the new data becomes the old data !
			Old_Data = New_Data;             
			}
		else{
			System.out.println("I am the same");
		}}}

Syntax error when changing the variables to "new" ...

Your are still using the operator != to compare Strings.
You should use the equals method.

Where are you using new in your posted code? new is used to create an instance. Putting it in a variable name has no special meaning. You could also use asdf to when defining a variable name.

have you tried running that bit of code I gave you? if you want to compare in light of equality of the value, you need to use the .equals() or the .equalsIgnoreCase() method.

== and != are only good when you're comparing the values of primitives.

let's say, in your case, you call: Am_I_Different(newData) and newData is declared and initialized as: String newData = new String("sameValueAsOld_Data");
even if the value is 'equal', it will always return true when using != , since these two String objects do not point to the same reference in your memory.

njah, I'm lousy at explaining, and I have no doubt someone will correct me if I said something wrong, but that's more or less what's wrong with your code :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.