Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

String Play ( In Beginner Java Programming) Write a program using Scanner and it

ID: 3871371 • Letter: S

Question

String Play ( In Beginner Java Programming)

Write a program using Scanner and its nextLine method. The following is an example of how to use nextLine

Scanner kybd = new Scanner(System.in);

System.out.println("Enter a line of text");

String aLine = kybd.nextLine();

You may assume that the user input has at least 7 characters.

Print either "the original String has no leading or trailing whitespace" or "the original String has leading or trailing whitespace." (Hint: the trim method will be a good start, but you'll need more.)

Swap the first three and last four characters of aLine and print the result.

Print aLine in all upper case.

Print the compareTo results of comparing aLine in all lower case with the original aLine. (This will be a number.)

Print whether the first half of aLine is the same as the last half of aLine except for case.   If aLine has an odd number of characters, the first half and last half are to the left and right of the middle character.

Note: treat each of these steps as an independent action operating on the original input. So if the original input was

kilroy was here, but not godot

swapping the first three and last four characters would print

odotroy was here, but not gkil

and printing aLine in all upper case would result in

KILROY WAS HERE, BUT NOT GODOT

Explanation / Answer

ScannerTest.java

import java.util.Scanner;

public class ScannerTest {

public static void main(String[] args) {

Scanner kybd = new Scanner(System.in);

System.out.println("Enter a line of text");

String aLine = kybd.nextLine();

aLine = aLine.trim();

System.out.println(aLine);

System.out.println("After swapping: ");

String swappedStr = aLine.substring(aLine.length()-4, aLine.length())+aLine.substring(3, aLine.length()-4)+aLine.substring(0,3);

System.out.println(swappedStr);

String upperStr = aLine.toUpperCase();

System.out.println("Uppper case string: ");

System.out.println(upperStr);

String lowerStr = aLine.toLowerCase();

System.out.println("Comparing aLine in all lower case with the original aLine: ");

System.out.println(aLine.compareTo(lowerStr));

System.out.println("The first half of aLine is the same as the last half of aLine except for case.: ");

if(aLine.length() % 2 != 0) {

System.out.println(aLine.substring(0,aLine.length()/2).equals(aLine.substring(aLine.length()/2+1, aLine.length())));

} else {

System.out.println(aLine.substring(0,aLine.length()/2).equals(aLine.substring(aLine.length()/2, aLine.length())));

}

}

}

Output:

Enter a line of text
kilroy was here, but not godot
kilroy was here, but not godot
After swapping:
odotroy was here, but not gkil
Uppper case string:
KILROY WAS HERE, BUT NOT GODOT
Comparing aLine in all lower case with the original aLine:
0
The first half of aLine is the same as the last half of aLine except for case.:
false