(JAVA) Be sure that you understand - how to use the java api\'s website to look
ID: 3786879 • Letter: #
Question
(JAVA)
Be sure that you understand
- how to use the java api's website to look up various Java classes and methods
- the difference between str = str.toUpperCase and just str.toUpperCase()
- the difference between the two versions of str.substring
Write a program with a main method that will
- write statement(s) to prompt the user to enter a String.
the prompt MUST be "Please enter a String: " and the user should enter it on the SAME LINE
- print one blank line
- print the string the user entered
- print whether the string is empty
- print the string's length
- print char at position 4
- print the index of the first 'a'
- print whether or not the string contains the string "Computer"
- print whether or not the string starts with the string "Computer"
- print whether or not the string equals "Computer" (ignore case)
- print how the string compares to "Computer"
- print the string's substring(3,6) BE CAREFUL.
- print the string's substring starting at position 3.
- CHANGE the string so that e's are replaced with x's. Don't print anything.
- print what the string would LOOK LIKE in lower case. Don't change it.
- print the string again
Explanation / Answer
import java.util.Scanner;
import java.lang.*;
public class St {
public static void main(String[] args) {
char ch;
int i;
Scanner inp = new Scanner(System.in);
System.out.println("Please enter a String:");
String data = inp.nextLine();
System.out.println("");
System.out.println("You entered:" + data);
if(data.length()==0){
System.out.println("String is empty");
return;
}else
System.out.println("The length of the String is : " + data.length());
ch = data.charAt(4);
System.out.println("The Char at Position 4 is : " + ch);
System.out.println("Index of first 'a' is : " + data.indexOf('a'));
if((-1 == data.indexOf("Computer"))){
System.out.println("String DOES NOT contain "Computer" string ");
}else
System.out.println("String HAS "Computer" string in it");
if((data.startsWith("Computer"))){
System.out.println("String Starts with "Computer" string ");
}else
System.out.println("String DOES NOT Starts with "Computer" string ");
if(data.equalsIgnoreCase("Computer")){
System.out.println("String Equals "Computer" ");
}else
System.out.println("String DOES NOT Equals "Computer" ");
/*System.out.println("Here the "Computer" String object" +
" is compared to the entered text, and if" + " the entered text contains the characters"
+ " in same order than equalsIgnoreCase method return true.");*/
i= data.compareTo("Computer");
if(i==0){
System.out.println("The two strings are Equal");
}
if(i<0){
System.out.println("The entered text comes before in dictionary than "Computer "");
}
if(i>0){
System.out.println("The entered text comes after "Computer " word in dictionary");
}
System.out.println("SubString(3,6) is : " + data.substring(3,6)); //including 3 but excluding 6th index
System.out.println("SubString starting at position 3 is : " + data.substring(3));
data.replace('e', 'x');
System.out.println("Lower Case is : " + data.toLowerCase());
System.out.println("You entered" + data);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.