Help Me With Java

DarkGhost368

Member!
Joined
Sep 12, 2004
Messages
118
Reaction score
0
Website
www.xboxarena.cjb.net
Hey, I was coding this do-while loop and it doesn't work properly. everytime I enter a valid entry (N,S,E,W) it repeats the loop. Can someone explain this to me? I'm still new too java.

Code:
char heading;
do{
	System.out.println("Enter Direction (N,E,S,W)");
	heading = IN.readLine().toUpperCase().charAt(0);
}while((heading != 'S')||(heading != 'E')||(heading != 'S')||(heading != 'E'));
 

Guest

Premium Member
Joined
Jun 28, 2003
Messages
3,905
Reaction score
2
Location
New york
Website
gamerz-lounge.com
Pm
x42bn6

ITS HIS ****ING JOB
 

SweatyOgre

BattleForums Senior Member
Joined
Jun 7, 2003
Messages
3,100
Reaction score
0
Location
PA
Replace your "or" conditionals with "and" conditionals. (&&)

Whenever you have a code problem just look at it logically. Your loop will repeat if the user hasn't entered N or S or E or W. So it will repeat unless the user has entered N, S, E and W which isn't possible.

Comp Sci major here, your whalecum.

Pm
x42bn6

ITS HIS ****ING JOB
Last I knew he was a student still, so he probably doesn't have a job programming in Java without a degree.
 

x42bn6

Retired Staff
Joined
Nov 11, 2002
Messages
15,150
Reaction score
2
Location
London, United Kingdom
Indeed, but I don't need a degree to help, do I?

Why do you need such stupid ways to read a character?

Code:
InputStreamReader reader=new InputStreamReader(System.in);
tmp=reader.read();
ch=(char) tmp;
And the ||s to &&s is correct. By de Morgan's Laws, (heading!='S')||(heading!='E')||(heading!='S')||(heading!='E')=!((heading=='S')&&(heading=='E')&&(heading=='S')&&(heading=='E'))=!false=true because heading cannot be multivalued at any one instance. Hence it will loop again, and again, and again, and again, and so on.*
 

NewPosts

New threads

Top