Question

Below is the assignment, I have completed and tested every step except for the last one....

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

  • Program checks the length of user input. If it is too short, the program prints a message. If it is sufficiently long, the specified operations are performed.
  • Program always uses the original input for each manipulation
  • Program uses only the specified String methods for manipulations
  • Program prints whether the String has leading or trailing whitespace
  • Program tests for any whitespace, not just spaces
  • Program swaps the first four and last three characters
  • Program prints the input in upper case
  • Program prints the results of String's compareTo method pn the original input and the original input converted to lower case
  • Program compares the first half and last half of the input, ignoring case
  • Program prints the original input with the first 'c' 'C' 'd' or 'D' removed

Homework Answers

Answer #1

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);

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Each person in the group will be responsible for rewriting the code to solve ​one​ of...
Each person in the group will be responsible for rewriting the code to solve ​one​ of the original parts (celebrity names), ​without using arrays or loops.​ The purpose of this assignment is to practice Strings manipulation and String methods so full credit will only be given if you are utilizing the code for Strings (substring, charAt, compareTo, equals, etc). program Java this was my last assigment Take the 4th and the 5th words from part 1 and print out both...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
Use python language please #One of the early common methods for encrypting text was the #Playfair...
Use python language please #One of the early common methods for encrypting text was the #Playfair cipher. You can read more about the Playfair cipher #here: https://en.wikipedia.org/wiki/Playfair_cipher # #The Playfair cipher starts with a 5x5 matrix of letters, #such as this one: # # D A V I O # Y N E R B # C F G H K # L M P Q S # T U W X Z # #To fit the 26-letter alphabet into...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
PYTHON : Create a Email Address Parser (* Please do make comments*) Often times, you may...
PYTHON : Create a Email Address Parser (* Please do make comments*) Often times, you may be given a list of raw email addresses and be asked to generate meaningful information from such a list. This project involves parsing such a list and generating names and summary information from that list. The script, eparser.py, should: Open the file specified as the first argument to the script (see below) Read the file one line at a time (i.e., for line in...