2 Replies - 3461 Views - Last Post: 01 December 2011 - 12:51 PM Rate Topic: -----

#1 Gniuz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-January 11

do while loop, comparing char to a char variable not working.

Posted 01 December 2011 - 12:42 PM

This is the last part of an assignment, it's not essential but would make my assignment look and work better. gen has been declared as a char variable and the do while loop is entered at least once. If the person enters an invalid char for gen then it should kick out an error message and re-enter the loop.

I'm using eclipse and its not giving me any errors. When running, it wont come out of the loop even if m, M, f or F is entered.

          char gen;

                do
		{
		System.out.print("Gender(m/f): ");
		gen = EasyIn.getChar();
		if(gen!='m'||gen!='M'||gen!='f'||gen!='F')
		{
			System.out.println("ERROR - Invalid gender");
		}
		}
		while(gen!='m'||gen!='M'||gen!='f'||gen!='F');


Any help will be much appriaciated, Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: do while loop, comparing char to a char variable not working.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: do while loop, comparing char to a char variable not working.

Posted 01 December 2011 - 12:46 PM

This will always be true

if(gen!='m'||gen!='M'||gen!='f'||gen!='F')

Assume that gen == 'M'

if 'M' not equal 'm' OR 'M' not equal 'M' OR 'M' not equal 'f' OR 'M' not equal 'F'

so at least 3 conditions that will make your statement TRUE :)
Was This Post Helpful? 0
  • +
  • -

#3 Gniuz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-January 11

Re: do while loop, comparing char to a char variable not working.

Posted 01 December 2011 - 12:51 PM

LOL, just relised this. Its cause the way I was saying it in my head the OR made sense.

Just done it on paper and saw the error of my ways.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1