Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > if statement

Reply
Thread Tools

if statement

 
 
geletine
Guest
Posts: n/a
 
      11-16-2005
I am a bigginner java programmer, thats obvious from the post, anyway i
am trying out this experiment, the program asks the users name, there
sex and there age.

the name is nothing more than a name, there sex determines weather they
can get a senior travel discount at 60 for females and 65 for males,
the standard discount can also be given to anyone 16 or under, anyone
older than 16 and younger than 60 cannot get a discount, anyone older
than 150 is considered dead.

I think i should be using two diffrent if statements, instead i have
combinded them together.

I start of by asking what there name is then there sex, if they are
male it asks there age and should decided weather there are 65 and
older to give them a senior bus card or under 65 but older than 16 to
give them no discount, if there 16 and younger a travelcard discount
and if they are over 150 there should be dead.

If they are not male then it asks if they are 60 or over, if they are
they get a senior travelcard if there 16 and younger a travelcard , if
they older than 16 but younger than 60 no travelcard and lastly if
there over 150 they should be dead.

here is my first attempt, :

import java.io.*;

public class bus

{



public static void main (String args [])throws IOException

{

String name, input, male;

int age;



BufferedReader info = new BufferedReader ( new InputStreamReader (
System.in ) );



System.out.println ("Enter your name");

input = info.readLine();

name = input;



System.out.println ("Are you male? ");

input = info.readLine();

male = input;



System.out.println ("Enter your age");

input = info.readLine();

age = Integer.parseInt ( input );



if ((male) || (age >= 65))

{

System.out.println ( name + " is entitled to a senior travelcard
");

}



else if ((male) || (age < 65))

{

System.out.println ( name + " is not entitled to a senior
travelcard ");

}



else if ((male) || ( age >= 150))

{

System.out.println ( name + " , you are kidding, what are you on?
");

}



else if (age >= 150)

{

System.out.println ( name + " , you are kidding, what are
you on ? ");

}



else if (age <= 16)

{

System.out.println ( name + " is entitled to a bus pass");

}



else if (age >= 60)
{
System.out.println (name + " is entitled to a senior bus
pass ");
}



else

{

System.out.println ( name + " is not allowed a bus pass");

}



}

}

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      11-16-2005
On 16 Nov 2005 07:39:23 -0800, "geletine" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>if ((male) |


you declared male as a string, presumably to contain "Y" "y" "male"
"M" or the like.

And you tried to use it as a boolean. You need to write some code to
convert from the string to the boolean.

You might do it with a switch

switch ( input.charAt(0) )
{
case 'y':
case 'm':
.....
male = true;
break;
.....

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
 
J. Verdrengh
Guest
Posts: n/a
 
      11-16-2005
Have a look at this:


import java.io.*;

public class Bus{
public static void main (String args []) throws IOException{
BufferedReader info = new BufferedReader ( new InputStreamReader
(System.in ) );

System.out.println ("Enter your name");
String name = info.readLine();

System.out.println ("Are you male? ");
String male= info.readLine();

System.out.println ("Enter your age");
int age = Integer.parseInt (info.readLine());

bool isMale = male.equals("yes");
int borderAge = -1;
if (isMale){
borderAge = 65;
} else {
borderAge = 60;
}


if (age <= 16) {System.out.println ( name + " is entitled to a bus
pass");}
else if(age>=borderAge && age<150) System.out.println ( name + " is
entitled to a senior travelcard");
else if(age<borderAge ) System.out.println ( name + " is not entitled
to a senior travelcard ");
else System.out.println ( name + " , you are kidding, what are you
on?");
}
}


 
Reply With Quote
 
J. Verdrengh
Guest
Posts: n/a
 
      11-16-2005
> bool isMale = male.equals("yes");

bool should be boolean


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-16-2005
On Wed, 16 Nov 2005 17:02:53 +0100, "J. Verdrengh" <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>int borderAge = -1;
> if (isMale){
> borderAge = 65;
> } else {
> borderAge = 60;
> }


Most java programmers would write that with the ternary operator as:

int borderAge = isMale ? 65 : 60;
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Monique Y. Mudama
Guest
Posts: n/a
 
      11-16-2005
On 2005-11-16, Roedy Green penned:
> On Wed, 16 Nov 2005 17:02:53 +0100, "J. Verdrengh" <(E-Mail Removed)>
> wrote, quoted or indirectly quoted someone who said :
>
>>int borderAge = -1;
>> if (isMale){
>> borderAge = 65;
>> } else {
>> borderAge = 60;
>> }

>
> Most java programmers would write that with the ternary operator as:
>
> int borderAge = isMale ? 65 : 60;


I guess I'm not most java programmers. I find the if/else syntax much
clearer. In fact, I just found a bug in someone else's code in which
the second and third arguments to the ternary operator were reversed;
I'd imagine the bug would have been more obvious if written using
if/else blocks instead.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      11-16-2005

"Monique Y. Mudama" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 2005-11-16, Roedy Green penned:
>> On Wed, 16 Nov 2005 17:02:53 +0100, "J. Verdrengh" <(E-Mail Removed)>
>> wrote, quoted or indirectly quoted someone who said :
>>
>>>int borderAge = -1;
>>> if (isMale){
>>> borderAge = 65;
>>> } else {
>>> borderAge = 60;
>>> }

>>
>> Most java programmers would write that with the ternary operator as:
>>
>> int borderAge = isMale ? 65 : 60;

>
> I guess I'm not most java programmers. I find the if/else syntax much
> clearer. In fact, I just found a bug in someone else's code in which
> the second and third arguments to the ternary operator were reversed;
> I'd imagine the bug would have been more obvious if written using
> if/else blocks instead.


Also, Java does some short circuiting with the ternary operator, so
that:

int borderAge = isMale ? getMaleBorderAge() : getFemaleBorderAge();

will only result in one method call, which may or may not be the
intention of the programmer (this is important if the methods have some sort
of side effect). Rewriting this as an if-else-statement usually makes the
intent explicit.

- Oliver


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-16-2005
On Wed, 16 Nov 2005 14:24:44 -0700, "Monique Y. Mudama"
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

>> int borderAge = isMale ? 65 : 60;

>
>I guess I'm not most java programmers. I find the if/else syntax much
>clearer. In fact, I just found a bug in someone else's code in which
>the second and third arguments to the ternary operator were reversed;
>I'd imagine the bug would have been more obvious if written using
>if/else blocks instead.


Objectively, the ternary form is clearer. There is less distracting
bubblegum and redundancy. What you really mean is the ternary form is
less familiar.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
Monique Y. Mudama
Guest
Posts: n/a
 
      11-16-2005
On 2005-11-16, Roedy Green penned:
> On Wed, 16 Nov 2005 14:24:44 -0700, "Monique Y. Mudama"
><(E-Mail Removed)> wrote, quoted or indirectly quoted someone
>who said :
>
>>> int borderAge = isMale ? 65 : 60;

>>
>>I guess I'm not most java programmers. I find the if/else syntax
>>much clearer. In fact, I just found a bug in someone else's code in
>>which the second and third arguments to the ternary operator were
>>reversed; I'd imagine the bug would have been more obvious if
>>written using if/else blocks instead.

>
> Objectively, the ternary form is clearer. There is less distracting
> bubblegum and redundancy. What you really mean is the ternary form
> is less familiar.


Objectively, I think you're wrong. I really mean that I think the
if/else syntax is clearer, just as I think that using curly braces and
parentheses even when they are not required by the compiler can make the
code clearer.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
 
Reply With Quote
 
Thomas G. Marshall
Guest
Posts: n/a
 
      11-17-2005
Oliver Wong coughed up:
> "Monique Y. Mudama" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On 2005-11-16, Roedy Green penned:
>>> On Wed, 16 Nov 2005 17:02:53 +0100, "J. Verdrengh" <(E-Mail Removed)>
>>> wrote, quoted or indirectly quoted someone who said :
>>>
>>>> int borderAge = -1;
>>>> if (isMale){
>>>> borderAge = 65;
>>>> } else {
>>>> borderAge = 60;
>>>> }
>>>
>>> Most java programmers would write that with the ternary operator as:
>>>
>>> int borderAge = isMale ? 65 : 60;

>>
>> I guess I'm not most java programmers. I find the if/else syntax much
>> clearer. In fact, I just found a bug in someone else's code in which
>> the second and third arguments to the ternary operator were reversed;
>> I'd imagine the bug would have been more obvious if written using
>> if/else blocks instead.

>
> Also, Java does some short circuiting with the ternary operator, so
> that:
>
> int borderAge = isMale ? getMaleBorderAge() : getFemaleBorderAge();
>
> will only result in one method call,


*Same* as with the if/else.


> which may or may not be the
> intention of the programmer (this is important if the methods have some
> sort
> of side effect). Rewriting this as an if-else-statement usually makes the
> intent explicit.


Huh? Explicit how? The if/else *might* have been arranged differently, but
then, so might the code containing the ?: ternary.


--
I've seen this a few times--Don't make this mistake:

Dwight: "This thing is wildly available."
Smedly: "Did you mean wildly, or /widely/ ?"
Dwight: "Both!", said while nodding emphatically.

Dwight was exposed to have made a grammatical
error and tries to cover it up by thinking
fast. This is so painfully obvious that he
only succeeds in looking worse.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
exec "statement" VS. exec "statement in globals(), locals() Ted Python 1 07-22-2004 08:51 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments