O'Reilly logo

Beginning Programming with Java For Dummies, 5th Edition by Barry A. Burd

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 12

Around and Around It Goes

IN THIS CHAPTER

check Creating program loops

check Formulating solutions to problems with loops

check 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 = keyboard.findWithinHorizon(".",0).charAt(0);c2 = keyboard.findWithinHorizon(".",0).charAt(0);c3 = keyboard.findWithinHorizon(".",0).charAt(0);c4 = keyboard.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 = keyboard.findWithinHorizon(".",0).charAt(0);c2 = keyboard.findWithinHorizon(".",0).charAt(0);c3 = keyboard.findWithinHorizon(".",0).charAt(0);c4 = keyboard.findWithinHorizon(".",0).charAt(0);c5 = keyboard.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 whenever the size of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required