aspose file tools*
The moose likes Beginning Java and the fly likes Assign message in boolean method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assign message in boolean method " Watch "Assign message in boolean method " New topic
Author

Assign message in boolean method

Amir Ahmed
Greenhorn

Joined: Feb 19, 2013
Posts: 17
The following code returns true or false based on the users age input. If user is over 21, it prints true. If not, it prints false. So Basically what I'm trying to do is assign a message to main that says " You can legally drink" if it is true and "You're too young to drink" if it is false. Tried few ways that i know but it kept changing the parameter userAge from int to string. Is there any possible way to assign the messages without change the parameter type from int to string.

Thanks!


class determineAge

public static void main(String[] args)
{

System.out.println("Enter your age?");
int age = readit.nextInt();
boolean age = yourAge(userAge);

System.out.println(age);// displays true or false only. How can i add a message depending on the users input?

private static boolean yourAge(int userAge)
{

if(userAge > 21)
{
return true;
}
else
{
return false;
}
}
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Hello Amir Ahmed ,
There can be multiple ways to accomplish this. I used the elvis operator to check the value of boolean variable. Its just the short form of if then else.



Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Amir Ahmed
Greenhorn

Joined: Feb 19, 2013
Posts: 17
Thank you so much Saif. Just tested it and worked fine. I very much appreciate it.
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Your welcome :-)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
What’s an elvis operator? Please always use the correct jargon.
Why are you using the ?: operator correctly on line 5 and not realising you have some very poor style on line 9. Look at this style guide, which tells you what you should have written.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

The ? : operator is not called the "elvis operator" in Java.

In the programming language Groovy, there's a thing called the elvis operator which is a shortened version of Java's ? : ternary operator. But Groovy's elvis operator doesn't exist in Java.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Why are you using the ?: operator correctly on line 5 and not realising you have some very poor style on line 9.


I was just re-using his code lines to emphasize the difference.
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

The ? : operator is not called the "elvis operator" in Java.


Yes you are right , it seems I confused it with Groovy. I double checked it immediately .
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Jesper de Jong wrote:In the programming language Groovy, there's a thing called the elvis operator which is a shortened version of Java's ? : ternary operator. But Groovy's elvis operator doesn't exist in Java.


I was about to ask why it's called an "Elvis operator", then took another look .

C# uses ?? as a similar construct.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Matthew Brown wrote: . . . I was about to ask why it's called an "Elvis operator", then took another look . . . .
Why? does it leave the building?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

the ? is supposed to be his hair and the : his eyes:

?:

also, this is logically wrong:



this would not let anyone under the age of 22 drink - almost a full year after they should. I know a LOT of college students who would not like this...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Amir Ahmed
Greenhorn

Joined: Feb 19, 2013
Posts: 17
fred rosenberger wrote:the ? is supposed to be his hair and the : his eyes:

?:

also, this is logically wrong:



this would not let anyone under the age of 22 drink - almost a full year after they should. I know a LOT of college students who would not like this...


Oh snap, i did not even notice that. Thanks for pointing it out!

Amir Ahmed
Greenhorn

Joined: Feb 19, 2013
Posts: 17
Just out of curiosity, is it possible to take the String str message assigned to "true" and " false" from main and put it in the boolean method? I just wanted to do all the math and assign the messages in the boolean method and have main display the message only (i.e "you can legally drink" or "you are too young to drink"). Not quite sure if it is possible or not. Any suggestions would be appreciated.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
That is called an out‑by‑one error. These are very common and surprisingly difficult to notice. The commonest type of that error is when a loop runs once too often or once too few times.

Yes, you can pass a command‑line argument. There are lots of different ways to do it: this is one possible way. You would have to check the API documentation for the valueOf method before using it:-Note this example creates a Boolean object which is unboxed by the Foo constructor. As I said, there are lots of other ways to do it.
Amir Ahmed
Greenhorn

Joined: Feb 19, 2013
Posts: 17
Thanks for your response Campbell. I have not covered this specific way in class so i have no clue how to write this code...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You can write it like thisAll you have to do is replace the two ellipses ... with something useful.Try it and see. That should work; I even showed you how to invoke it at the command line.
Amir Ahmed
Greenhorn

Joined: Feb 19, 2013
Posts: 17
Awesome. It worked flawlessly. Now I know what to do when I go over this in class after spring break. Thank you so much Campbell.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You’re welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Assign message in boolean method