Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in a single class called DoubleLetters that does the following:

ID: 3670977 • Letter: W

Question

Write a program in a single class called DoubleLetters that does the following:

The program should read in a single line of text from the keyboard. It should then print to the console the same line of text with these changes: Letters should be doubled. Exclamation points (!) should be tripled. All other characters appear as they do in the input string.

The objectives of this assignment are to:

Be able to read text input from the keyboard using a Scanner object.

Be able to traverse the characters in a String using String class methods and a for loop.

Be able to use an if statement in a loop.

Example 1: if the input text is:

Then your program should print:

Example 2: if the input text is:

Then your program should print:

note: concatenating a char onto a String requires that you add an empty String so the char is treated as a character and not a number. Example: newStr = newStr + "" + curChar; Another solution (and a better one) is to use the StringBuilder class. See the Java API for more on this class.

Hint:
When is a character a letter? There is more than one way to tell. Look up the Character class in the Java API. There is a method that would be very helpful.

Explanation / Answer

public class HelloWorld {

   public static void main(String[] args) {
       DoubleLetters dl = new DoubleLetters();
       dl.DoDoubling();
   }

}


DoubleLetters.java

import java.util.Scanner;

public class DoubleLetters {
  
   private final char SPECIAL_CHAR_EXCALAM = '!';
  
   public void DoDoubling() {
      
       Scanner in = new Scanner(System.in);
       String input = in.nextLine();
      
       int length = input.length();
       StringBuffer sb = new StringBuffer();
      
       for (int i = 0; i < length; i++) {
           sb.append(input.charAt(i));
           if (isAlphabet(input.charAt(i))) {
               sb.append(input.charAt(i));
           }
           if (input.charAt(i) == SPECIAL_CHAR_EXCALAM) {
               sb.append(input.charAt(i));
               sb.append(input.charAt(i));
           }
       }
       System.out.println(sb);
   }
  
   private boolean isAlphabet(char chr) {
       int asciiChar = chr;
       return (asciiChar >= 65 && asciiChar <= 90)
               || (asciiChar >= 97 && asciiChar <= 122);
   }
}

output

Hey!My car can't move!!                                                                                                                                     
HHeeyy!!!MMyy ccaarr ccaann'tt mmoovvee!!!!!!                                                                                                               

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote