Java if statment doesnt seem to be working corrently

hi all i have written a if else statement, it all compiles with no issues and the program runs with no issues at all, but it doesnt matter what i enter it will go to the last else statment even if it matchs the statment

i have added the code if u could please help  
import java.util.Scanner;
import java.text.DecimalFormat;



public class Point

{
	public static void main (String [] args)

	{

	Double x = 0.0; // This will store the value for the x Plane
	Double y = 0.0; // This will store the value for the y plane
	String direction; // This will store the keyboard input from the user input
	

	//This will creat the scanner object to read from the keyboard
	Scanner Keyboard = new Scanner (System.in);

	//This will create formattion for the output of x and y 
	DecimalFormat formatter = new DecimalFormat ("#0");
	
	System.out.println ("This program updates position of a traveling point in a square");

	
	do
	    {
		System.out.println ("Starting position of the point is " + "(" +(formatter.format(x)) + "," +(formatter.format(y)) + ")" );
		System.out.print ("Enter a direction for the point to move (u, d, l or r):");
		direction = Keyboard.nextLine();

		if (direction == "r" || direction  == "R")
		    {
			x = (x + 1);
		    }		
		else if (direction == "l" || direction == "L")
		    {
			x = (x - 1);
		    }
		else if (direction == "u"  || direction == "U")
		    {
			y = (y + 1);
		    }
		else if (direction == "d" || direction ==  "D")
		    {
			y = (y - 1);
		    }
		else
		    {
			System.out.println("Invalid entry, try again");
		    }
	    } while (direction != "q");
	}

}

Open in new window

awolarczukAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ErdyMurphyCommented:
if you want to compare strings... you have to use the equals method...
otherwise you comparing the references....
ErdyMurphyCommented:
Example:
direction.equals("r)

Open in new window

for_yanCommented:
String vaues should be compared using equals()
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

ErdyMurphyCommented:
i forgot a -> "
cmalakarCommented:
as you are comparing for both upper and lower case you can do it in one of the following ways..
if(direction.equalsIgnoreCase("r")){
//do something..
}else if(...


or 

if(direction.toLoweCase().equals("r")){
...
}else if(...

Open in new window

Sathish David Kumar NArchitectCommented:
direction .equalsIgnoreCase(r)----------? it will check both upper and lower
for_yanCommented:

this is a good explanation of == vs equals and also explains why in some cases == can happen to be
true:

http://stackoverflow.com/questions/767372/java-string-equals-versus
awolarczukAuthor Commented:
ok got it working but my while loop doesnt seem to work how do i get that partworking now
for_yanCommented:
Did you change to
 while (!direction.equals("q"))
In that part?
awolarczukAuthor Commented:
ok i need it to work like this how do i do that

  }while (x != 4 || x != -4 || y !=4 || y !=-4);      
ErdyMurphyCommented:
could it be that you want frequently to ask which key a user pressed?
because if you want to something like that you did it the wrong way...
you have to use a so called keylistener which is automatically activated.
what you are doing is polling until the user has pressed a key...
awolarczukAuthor Commented:
narr not after that i just want to know when x = 4 or - 4 or y = 4 or - 4 and when the while loop
for_yanCommented:
so you want to go out of the loop when at least one of these
conditions is true? Say, you want to finish is x is equal to 4 or when
x is equal to -4, etc.

then you probably what to have AND's rather than OR's

So you should sit in the loop
if all these conditions are true simultaneously that x != 4 and x != -4 and y != 4 and y != -4
so it should be
while (x != 4 && x != -4 && y !=4 && y !=-4);    

the way you wrote it

 (x != 4 || x != -4 || y !=4 || y !=-4) this would be always true as x cannot be simultaneously 4 and -4
     
 

awolarczukAuthor Commented:
ok mate that seemed to work but i need thsi message to print on the screen

      else if (y == 4 || y == -4)
                {
                  System.out.println("The point reached the boundary at" + "(" +(formatter.format (x)) + "," + (formatter.format(y)) + ")" + " after " + (formatter.format (count)) + " moves." );
                }

before it exits
for_yanCommented:

You can do it right before this line:


 } while (direction != "q");

and you don't need "else"

if (y == 4 || y == -4)
                {
                  System.out.println("The point reached the boundary at" + "(" +(formatter.format (x)) + "," + (formatter.format(y)) + ")" + " after " + (formatter.format (count)) + " moves." );




import java.util.Scanner;
import java.text.DecimalFormat;



public class Point

{
	public static void main (String [] args)

	{

	Double x = 0.0; // This will store the value for the x Plane
	Double y = 0.0; // This will store the value for the y plane
	String direction; // This will store the keyboard input from the user input
	

	//This will creat the scanner object to read from the keyboard
	Scanner Keyboard = new Scanner (System.in);

	//This will create formattion for the output of x and y 
	DecimalFormat formatter = new DecimalFormat ("#0");
	
	System.out.println ("This program updates position of a traveling point in a square");

	
	do
	    {
		System.out.println ("Starting position of the point is " + "(" +(formatter.format(x)) + "," +(formatter.format(y)) + ")" );
		System.out.print ("Enter a direction for the point to move (u, d, l or r):");
		direction = Keyboard.nextLine();

		if (direction == "r" || direction  == "R")
		    {
			x = (x + 1);
		    }		
		else if (direction == "l" || direction == "L")
		    {
			x = (x - 1);
		    }
		else if (direction == "u"  || direction == "U")
		    {
			y = (y + 1);
		    }
		else if (direction == "d" || direction ==  "D")
		    {
			y = (y - 1);
		    }
		else
		    {
			System.out.println("Invalid entry, try again");
		    }

if (y == 4 || y == -4)
                {
                  System.out.println("The point reached the boundary at" + "(" +(formatter.format (x)) + "," + (formatter.format(y)) + ")" + " after " + (formatter.format (count)) + " moves." );
}

	    } while (....);
	}

}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.