Question

Write a program that prompts a user, using Scanner, for 2 secret messages. Each message has...

Write a program that prompts a user, using Scanner, for 2 secret messages. Each message has its own set of rules you need to use to decode it. You will use String methods to change the messages you receive from the user.

*Note: Ensure that you make the changes in the order they are specified and do the operations on the updated String

Sentence 1:

  • Make everything after the first word lowercase
  • Remove the last space
  • Remove the first 3 characters
  • Change every z to an m
  • Change every w to a space
  • Find the first a and change it to a g

Sentence 2:

  • Trim the extra white space from the sentence
  • Replace every instance of asdf with the second character in the sentence

Take the first 8 characters and put them at the end of the sentence

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any problem with the code feel free to comment. Since no test cases were provided with the code, I was not able to test the program.

Program

import java.util.Scanner;

public class Test {
   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       // taking user input
       System.out.println("Enter the first Sentence: ");
       String first = sc.nextLine();

       System.out.println("Enter the second sentence: ");
       String second = sc.nextLine();

       System.out.println("Decode message 1: " + decodeMessage1(first));
       System.out.println("Decode message 2: " + decodeMessage2(second));

       sc.close();

   }

   public static String decodeMessage1(String msg) {

       // converting the entire sententence to lower case except first character
       String temp = msg;
       msg = msg.toLowerCase();
       msg = msg.replaceFirst(msg.charAt(0) + "", temp.charAt(0) + "");

       // remove the last space
       int lastSpaceIndex = msg.lastIndexOf(" ");
       String first = msg.substring(0, lastSpaceIndex);
       String second = msg.substring(lastSpaceIndex + 1, msg.length());
       msg = first + second;

       // remove first 3 characters
       msg = msg.substring(3, msg.length());

       // replace ever 'z' to 'm'
       msg = msg.replace('z', 'm');

       // change every 'w' to a space
       msg = msg.replaceAll("w", " ");

       // find first "a" and change it to "ag"
       msg = msg.replaceFirst("a", "a g");

       return msg;
   }

   public static String decodeMessage2(String msg) {

       // triming the extra space
       msg = msg.trim();

       // replacing all asdf with second character of the sentence
       msg = msg.replaceAll("asdf", msg.charAt(1) + "");

       // taking first 8 charcter and put them at the end of the sentence
       String first = msg.substring(0, 8);
       String second = msg.substring(8, msg.length());
       msg = second + first;

       return msg;
   }
}

Add a comment
Know the answer?
Add Answer to:
Write a program that prompts a user, using Scanner, for 2 secret messages. Each message has...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • (Process a string) Write a program that prompts the user to enter a string and displays...

    (Process a string) Write a program that prompts the user to enter a string and displays its length and its first character. java StringLength Enter a string:Does quantum determinacy have anything to with whether human cognition is deterministic? The string is of length 88 and the first character is D import java.util.Scanner; class StringLength{    public static void main (String args[]){        Scanner input = new Scanner(System.in);        System.out.println("Enter a string:");        String ans = input.nextLine();        System.out.println("The string...

  • Write a program that separately prompts the user for a first name and last name and...

    Write a program that separately prompts the user for a first name and last name and outputs a string containing the following information, in order: a. First letter of the user's name. b. First five letters of the user's last name. c. A random two-digit integer You must construct the desired string ensuring all characters are lowercase; output the identification string accordingly. Assume the last name contains at least 5 characters. You must use the Random (java.util.Random) class to generate...

  • C Code Create a tool in which a user can enter a string (up to 25...

    C Code Create a tool in which a user can enter a string (up to 25 characters) and choose how they wish to manipulate the string from a menu your program will display (see the run-through). The program will continue to ask for commands until the user enters the “quit” command. The commands are: • “Replace All” If a user types “replace all” using ANY sort of capitalization, your program will prompt the user to enter the character to change...

  • String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line wh...

    String variables/Selection & loop Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline(), converts the string into Ubbi dubbi and displays the translated sentence. Ubbi dubbi works by adding ub before each vowel sound in a syllable. For example, hello becomes hubellubo. The word speak has the vowel sound ea, so in Ubbi dubbi it becomes spubeak....

  • Use C++ language, keep it simple Exercise #2: Strings' operations Write a C++ program that prompts...

    Use C++ language, keep it simple Exercise #2: Strings' operations Write a C++ program that prompts the user to enter a sentence, the program then does the following: 1. Prints the total characters in the sentence. 2. Prints the word count of the sentence, assuming only one space between each tow words. 3. Prints total vowel (a, e, i, o, u) in the sentence. 4. Prints the vowel frequency as shown in the sample below. 5. Prints the sentence in...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

  • Write a Java Program that performs the following functionalities: and you can use if statements, cases,...

    Write a Java Program that performs the following functionalities: and you can use if statements, cases, and string methods Enter a new main sentence Find a String Find all incidents of a String Find and Replace a String Replace all the incidents of a String Count the number of words Count a letter’s occurrences Count the total number of letters Delete all the occurrences of a word Exit The program will display a menu with each option above, and then...

  • Write a C program that prompts the user to enter a series of integers that represent...

    Write a C program that prompts the user to enter a series of integers that represent a coded message. I have given you the decoder program so that you can see how it is written using array notation. Your assignment is to convert the code to a program that uses only pointers and pointer math. Your program must use pointer notation and pointer math (i.e. no array index notation other than in the declaration section, no arrays of pointers and...

  • Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains...

    Finish FormatJavaProgram.java that prompts the user for a file name and assumes that the file contains a Java program. Your program should read the file (e.g., InputFile.java) and output its contents properly indented to ProgramName_Formatted.java (e.g., InputFile_Formatted.java). (Note: It will be up to the user to change the name appropriately for compilation later.) When you see a left-brace character ({) in the file, increase your indentation level by NUM_SPACES spaces. When you see a right-brace character (}), decrease your indentation...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT