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

Any help on this problem? I\'ve been working on it for hours and I can\'t get it

ID: 3550339 • Letter: A

Question

Any help on this problem? I've been working on it for hours and I can't get it.



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, only now with every letter doubled. Other characters in the line should be left alone, except for exclamation points (!): These should be tripled. Example: if the input line is: Hey! My car can't move!! Then your program should print: HHeeyy!!! MMyy ccaarr ccaann'tt mmoovvee!!!!!! Make sure that you comment your code (in places just comment groups of lines here - so you might have a comment that simply says "Reads the input" for the statements that do the prompting and reading). Note: when is a character a letter? If ch is a char variable, then you can determine if ch holds a letter (upper or lower case, A-Z, a-z) with this method call: Character.isLetter(ch) This method returns a boolean value (true means: ch IS a letter; false means it isn't) Do NOT include import statements in your submission. We input the Scanner class for you. import java.util.Scanner;

Explanation / Answer

import java.util.Scanner;
public class DoubleLetters
{
public static void main(String[] args)
{
//create scanner object.
Scanner in = new Scanner(System.in);
System.out.println("Enter a line of text :");
// read line of text from user.
String text = in.nextLine();
// start processing each letter
for(int i=0; i<text.length(); i++)
{
// get letter at position i
char ch = text.charAt(i);
// if it is character print double
if(Character.isLetter(ch))
System.out.printf("%c%c",ch,ch);
else if(ch=='!') // if it is exclamatory mark print triple
System.out.printf("%c%c%c",ch,ch,ch);
else // else print single.
System.out.printf("%c",ch);
}
}
}

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