0

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 ---

Edited by RLS0812: n/a

3
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by stultuske
0

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

0

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

0

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");
0

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");
		}}}
0

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.

Edited by NormR1: n/a

0

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 :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.