You are given the statement: String s1 = input.nextLine();//where input is a sca
ID: 3792322 • Letter: Y
Question
You are given the statement: String s1 = input.nextLine();//where input is a scanner object Assigning the user enters arbitrary tort as input: given the Java statement(s) required to compete the following tasks Determine and store in a variable me total number of characters in the String Determine and store in one String two copies of the first character in String $1 (i e., it the first character is 'a', the new string should be 'aa') Write a complete if statement that prints "I love COSC" if s1 is "I am happy!" Create a new stung s2 that contains only the last three characters of s1 (assume there are enough characters in s1 to accomplish this).Explanation / Answer
1)
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("enter the string");
String s1=scanner.nextLine();
//to determine total no of characters in the string
int count=s1.length();
System.out.println("the total no of characters in the above entered string is "+count);
}
}
output
enter the string
bob is a king maker
the total no of characters in the above entered string is 19
b)
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("enter the string");
String s1=scanner.nextLine();
//to store copy of the string
String s2=s1.substring(0, 1);
//to make duplicate string
String s3=s2+s1;
System.out.println("the duplicate string is "+ s3);
}
}
output
enter the string
iam happy
the duplicate string is iiam happy
c)
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("enter the string");
String s1=scanner.nextLine();
if(s1.trim().equals("i am happy!"))
{
System.out.println("the string is "+"iam cosc");
}else
System.out.println("the original is "+s1);
}
}
output
enter the string
i am happy!
the string is iam cosc
d)
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("enter the string");
String s1=scanner.nextLine();
int length=s1.length();
String s2=s1.substring(length-3, length);
System.out.println("the string is "+s2);
}
}
output
enter the string
iam happy
the string is ppy
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.