Link to home
Start Free TrialLog in
Avatar of awolarczuk
awolarczukFlag for Australia

asked on

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

Avatar of ErdyMurphy
ErdyMurphy

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

Open in new window

String vaues should be compared using equals()
i forgot a -> "
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

Avatar of Sathish David  Kumar N
direction .equalsIgnoreCase(r)----------? it will check both upper and lower

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
Avatar of awolarczuk

ASKER

ok got it working but my while loop doesnt seem to work how do i get that partworking now
Did you change to
 while (!direction.equals("q"))
In that part?
ok i need it to work like this how do i do that

  }while (x != 4 || x != -4 || y !=4 || y !=-4);      
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...
narr not after that i just want to know when x = 4 or - 4 or y = 4 or - 4 and when the while loop
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
     
 

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
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial