Below is the assignment, I have completed and tested every step except for the last one.
"Print aLine with the first 'c' 'C' 'd' or 'D' removed."
any help would be appreciated
ASSIGNMENT
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 four and last three 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.
Print aLine with the first 'c' 'C' 'd' or 'D' removed.
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 four and last three characters would print
dotoy was here, but not gokilr
and printing aLine in all upper case would result in
KILROY WAS HERE, BUT NOT GODOT
You may use ONLY String's indexOf, charAt, length, compareTo, toUpperCase, toLowerCase, trim, equals, equalsIgnoreCase and substring methods.
Grading Elements
If you have any doubts, please give me comment...
import java.util.Scanner;
public class StringPlay{
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
System.out.println("Enter a line of text");
String aLine = kybd.nextLine();
int cInd = aLine.toLowerCase().indexOf('c');
int dInd = aLine.toLowerCase().indexOf('d');
int firstInd = (cInd<dInd && cInd!=-1)?cInd:dInd;
if(firstInd!=-1)
aLine = aLine.substring(0, firstInd)+aLine.substring(firstInd+1);
System.out.println(aLine);
}
}
Get Answers For Free
Most questions answered within 1 hours.