Checking a String

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

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

Checking a String

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:

two way decision
C:>javac RainTester.java

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

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

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

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

QUESTION 4:

Is the integer -12 negative or not?