Programming Project: Double Letters Description: Write a program in a single cla
ID: 3873529 • Letter: P
Question
Programming Project: Double Letters Description: 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-upper or lower case- should be doubled. Letters are the characters a through z (lower case), and A through Z (upper case). Exclamation points (!) should be tripled. All other characters appear as they do in the input string. Objectives: 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. Examples Use these examples to test your code Example1 Enter a line of text > Hey My car can't movel l Then your program should print: HlHeeyyI1 MMyy ccaarr ccaann'tt mmooveeL Example 2: if the input text is: Enter a line of text > Hello, I have 9 of them! Then your program should print:Explanation / Answer
Java Code:
import java.util.Scanner;
public class DoubleLetters
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in); //Creating an object of Scanner class
System.out.println("Enter a line of text");
String text=sc.nextLine();
int i,length=text.length();
for(i=0;i<length;i++)
{
if(Character.isLetter(text.charAt(i)))
System.out.print(""+text.charAt(i)+text.charAt(i));
else if(text.charAt(i)=='!')
System.out.print("!!!");
else
System.out.print(text.charAt(i));
}
System.out.println();
}
}
Important Note: It is given that import statements will be provided by your evaluators. Hence, please remove the import statement. I have added that to just run and check my program and also so that you can copy paste and run it and check the output
Code Explanation:
import java.util.Scanner;
public class DoubleLetters
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in); //Creating an object of Scanner class
System.out.println("Enter a line of text"); //Prompting the user for input
String text=sc.nextLine(); //reading the input
int i,length=text.length();
for(i=0;i<length;i++) //looping through the whole string
{
if(Character.isLetter(text.charAt(i))) // checking if character is a letter using the Character.isletter() method
System.out.print(""+text.charAt(i)+text.charAt(i)); //Printing the character twice
else if(text.charAt(i)=='!') //if it is an exclamation,printing it thrice
System.out.print("!!!");
else
System.out.print(text.charAt(i)); //printing the character at index 'i' once, if it is not a character or an exclamation
}
System.out.println(); //printing new line
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.