File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How?

 
Lenny Peter
Ranch Hand
Posts: 52
  • 0
  • 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: 5115
11
Java Netbeans IDE Opera
  • 0
  • 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
  • 0
  • 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: 3380
  • 0
  • 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: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better title will also help
 
Arvind Mahendra
Ranch Hand
Posts: 1162
  • 0
  • 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: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic