Given the String object calledmyString with the value \"Now is the time for the
ID: 668525 • Letter: G
Question
Given the String object calledmyString
with the value "Now is the time for the birthday party"
show the following steps
Write a statement that will print the number of characters in the string.
Write a statement that outputs the character at index 16
Write a statement that outputs the position of the word “for” in the sentence.
Write a statement that outputs the sentence in uppercase letters
Write a statement that uses the original string to extract the new string "birthday party" and prints it to the screen
Explanation / Answer
import java.text.NumberFormat;
import java.util.Currency;
import java.util.Scanner;
public class Assignment2
{
public static void main(String[] args)
{
String myString = "Now is the time for the birthday party";
// Statement to print the number of characters in the string.
// Use .length and declare an int to store the number in.
int myStringLength = myString.length();
System.out.println("Number of characters in the string: " + myStringLength);
// Statement that outputs the character at index 16.
// Declaring char to hold character in and getting value.
char sixteen = myString.charAt(16);
System.out.println("The character at index 16 is: " + sixteen);
// Statement that outputs the position of the word "for" in the sentence.
int position = myString.lastIndexOf("for");
System.out.println("The position of the word 'for' in the sentence is: " + position);
// Statement that outputs the sentence in uppercase letters.
System.out.println(myString.toUpperCase());
// Extract the new string "birthday party" and print it to screen.
String extraction = myString.substring(24);
System.out.println("The new string is: " + extraction);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.