I've come up with some code to try and reject ages outside the range of 18 to 64, but i cant seem to find my errors or how to fix it. Feel stupid as its probably somethin easy, but i'm just learnin the language, can you help:

int age;
age = getIntFromUser(); // assume this function works correctly
if(age < 18 && > 65){
System.out.println("age is accepted: please continue");
}
else {
System.out.println("age rejected: program terminating");
}

Recommended Answers

All 3 Replies

Greetings.

if (age<18 || age>64)
  // do something
else
  // do something

I think you should use the OR operator instead of AND because if it is rather impossible to get a number which is <18 and >64 altogether.
;)

* Edit:
LOL, just saw nosani's reply right after mine. Yeah, the arrows that he mentioned is right. So, mine is the opposite, eh. You would have to print error message for the if part, and an "accepted" message for the else part.

if(age < 18 && > 65){
Replace this with :
if(age > 18 && < 65){
You got the wrong arrows ... it says age less than 18 and greater than 65 is accepted ... LOL

Cheers guys - how stupid do i feel now. told ya it'd be somethin simple.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.