• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assign message in boolean method

 
Amir Ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Amir Ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Saif. Just tested it and worked fine. I very much appreciate it.
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your welcome :-)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Amir Ahmed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49849
70
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic