Use various String methods to complete the class Printer given below. And it nee
ID: 3786290 • Letter: U
Question
Use various String methods to complete the class Printer given below. And it need to be first run with the given word(separate) but then it will run again with a different word.
1.print 3 times the length of the given word (use a string method and do not just print a number). Note: There is a String method length() which returns the length of the String
2.print the uppercase version of the word (do not change the original word)
3.replace "r" with "12", "a" with "4", and "e" with "3" in the original word. You will need to use the replace method three times. Do not change the original word itself. Do not declare more than one new variable
4.print the string with all the replacements,
5.print the original word.
--------------------------------------------------------------------------------------------
public class Printer
{
public static void main(String[] args)
{
String word = "separate";
}
}
Explanation / Answer
Printer.java
public class Printer
{
public static void main(String[] args)
{
String word = "separate";
for(int i=0; i<3; i++){
System.out.println(word.length());
}
System.out.println(word.toUpperCase());
String newStrign = word.replace("r", "12");
newStrign = newStrign.replace('a', '4');
newStrign = newStrign.replace('e', '3');
System.out.println(newStrign);
System.out.println(word);
}
}
Output:
8
8
8
SEPARATE
s3p4124t3
separate
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.