Chapter 12

Around and Around It Goes

In This Chapter

arrow Creating program loops

arrow Formulating solutions to problems with loops

arrow Diagnosing loop problems

Chapter 8 has code to reverse the letters in a four-letter word that the user enters. In case you haven’t read Chapter 8 or you just don’t want to flip to it, here’s a quick recap of the code:

c1 = myScanner.findWithinHorizon(“.”,0).charAt(0);

c2 = myScanner.findWithinHorizon(“.”,0).charAt(0);

c3 = myScanner.findWithinHorizon(“.”,0).charAt(0);

c4 = myScanner.findWithinHorizon(“.”,0).charAt(0);

System.out.print(c4);

System.out.print(c3);

System.out.print(c2);

System.out.print(c1);

The code is just dandy for words with exactly four letters, but how do you reverse a five-letter word? As the code stands, you have to add two new statements:

c1 = myScanner.findWithinHorizon(“.”,0).charAt(0);

c2 = myScanner.findWithinHorizon(“.”,0).charAt(0);

c3 = myScanner.findWithinHorizon(“.”,0).charAt(0);

c4 = myScanner.findWithinHorizon(“.”,0).charAt(0);

c5 = myScanner.findWithinHorizon(“.”,0).charAt(0);

System.out.print(c5);

System.out.print(c4);

System.out.print(c3);

System.out.print(c2);

System.out.print(c1);

What a drag! You add statements to a program ...

Get Beginning Programming with Java® For Dummies®, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.