go to previous page   go to home page   go to next page hear noise highlighting

Answer:

If the user enters anything other than exactly the single character Y then the "false branch" is executed.


Checking a String

two way decision

The if statement

if ( answer.equals("Y") )            

picks either the "true branch" or the "false branch" . Only one branch or the other is executed, just as in the flow chart. This part of the statement

answer.equals("Y")           

evaluates to true if the string referenced by answer contains exactly the single character "Y" . For anything else it evaluates to false. This is somewhat awkward. Dealing with user input is often awkward. Later on you will see better ways to do this. Here are some runs of the program:

C:\Examples>javac RainTester.java

C:\Examples>java RainTester
Is it raining? (Y or N): Y
Wipers On

C:\Examples>java RainTester
Is it raining? (Y or N): N
Wipers Off

C:\Examples>java RainTester
Is it raining? (Y or N): Yes
Wipers Off

C:\Examples>java RainTester
Is it raining? (Y or N): Rats
Wipers Off

QUESTION 4:

Is the integer -12 negative or not?


go to previous page   go to home page   go to next page