Question
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
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();
If the user's input is shorter than 7 characters, print the message "The input is too short" and do no further processing. If the user's input is 7 characters or longer, perform the following operations.
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 two and last five characters of aLine and print the result.
Print aLine in all upper case.
If aLine has an odd number of characters, print "The line has an odd number of characters." Otherwise, print the two middle characters of aLine.
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.
Print aLine with one character removed: The first 'e' 'E' 's' or 'S'.
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 two and last five characters would print
Godotlroy was here, still waiting for Ki
and printing aLine in all upper case would result in
KILROY WAS HERE, BUT NOT GODOT
You may use ONLY indexOf, charAt, length, compareTo, toUpperCase, toLowerCase, trim, equals, equalsIgnoreCase and substring methods.
sample output from my solution is
Enter a line of text: This line starts with a blank The original String has leading or trailing whitespace First 2 and last 5 chars swapped: blankhis line starts with a T to upper case: THIS LINE STARTS WITH A BLANK The two middle characters are rtcompareTo lower case version: -32 Does the fist half equal the last half? false the input with first 'e' or 'E' or 's' or 'S' removed: Thi line starts with a blank Enter a line of text: Kilroy was here, still waiting for Godot The original String has no leading or trailing whitespace First 2 and last 5 chars swapped: Godotlroy was here, still waiting for Ki to upper case: KILROY WAS HERE, STILL WAITING FOR GODOT The two middle characters are ilcompareTo lower case version: -32 Does the fist half equal the last half? false the input with first 'e' or 'E' or 's' or 'S' removed: Kilroy wa here, still waiting for Godot Enter a line of text: reiterate reiterate The original String has leading or trailing whitespace First 2 and last 5 chars swapped: erateeiteratereit r to upper case: REITERATE REITERATE The two middle characters are e compareTo lower case version: 0 Does the fist half equal the last half? true the input with first 'e' or 'E' or 's' or 'S' removed: riterate reiterate
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started