Question: Consider the flawed method printLetters that follows, which accepts a String as its parameter and attempts to print the letters of the String , separated

Consider the flawed method printLetters that follows, which accepts a String as its parameter and attempts to print the letters of the String , separated by dashes. For example, the call of printLetters("Rabbit") should print R-a-b-b-i-t . The following code is incorrect:

public static void printLetters (String text) { for (int i = 0; i < text.length (); i++) { !! System.out.print (text.charAt (i) +

What is wrong with the code? How can it be corrected to produce the desired behavior?

public static void printLetters (String text) { for (int i = 0; i < text.length (); i++) { !! System.out.print (text.charAt (i) + "-"); } System.out.println (); // to end the line of output

Step by Step Solution

3.37 Rating (166 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The printLetters code has a fencepost problem it will print a das... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Building Java Programs A Back to Basics Approach Questions!