Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How?

 
Lenny Peter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I write this correctly?

public void setAge(int age)
{
if(age > 0 && age < 18) {
System.out.println("Age:" + age + " " + "this member is junior.");
}
else if(age >= 18) {
System.out.println("Age:" + age +" " + "this member is senior.");
}
else {
System.out.println("The member's age cannot be 0");
}
}

Thanks ;-)
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umm, so what's wrong with the code you posted? (apart from the unfortunately named method that doesn't in fact set a int field named age.)
 
Gary Morris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I don't see why it couldn't work as-is, but would be more meaningful to give it a proper name like:


[ May 11, 2008: Message edited by: Gary Morris ]
 
arulk pillai
Author
Ranch Hand
Posts: 3388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also use of log4j instead of System.out.println might be better.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15440
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method is called 'setAge', but I don't see you setting the entered age anywhere. You only check the value of the parameter. Maybe you intended to save the value in a member variable?
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I see that if someone passes a negative int to the method, they will get a reply "The age cannot be 0." They might say, "I didn't say 0; I said -7!"
 
Gary Morris
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe if you gave a little more info on what it is that you WANT it to do, then someone could offer a little better advice?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better title will also help
 
Arvind Mahendra
Ranch Hand
Posts: 1162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you also don't need those braces with every if else since you only have single statements.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arvind Birla:
you also don't need those braces with every if else since you only have single statements.

Though many coding conventions state that it's best to include them regardless.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic