Question

JAVA PROGRAM Write a program that contains the following two methods Write a method that accepts...

JAVA PROGRAM

Write a program that contains the following two methods

Write a method that accepts a user string as a parameter and converts any words that are in all uppercase to all lowercase.

Note: the first letter at the start of a sentence should remain in upper case if that is what the user enters. See example below.

Enter Initial Text: (prompts user for text)

Currently: HEY! What do you think about this CRAZY STUFF!! WHAT a day.

Revised text: Hey! What do you think about this crazy stuff!! What a day.

Write a method that accepts a user string and returns the converted string. Certain (target) words are replaced with replacement words. (CANNOT USE REPLACE ALL or ARRAYS)

Target Word (case insensitive)

  • Replacement words
  • tbh/TBH/Tbh
  • To be honest
  • idiot
  • silly
  • stupid/ stupidly
  • unwise /unwisely
  • yo
  • hey
  • fake
  • innovative
  • dope
  • exciting

Things to watch out for

Embedded words - Maybe someone embedded one of those words in some other word. For example, the tweet "That was a real fakeout by their forward" should not be converted to "That was a real innovativeout by their forward.”

Constants - You should declare appropriate class constants (e.g., IDIOT_REPLACE) for the replacement words.

Example

Enter initial tweet: His name should be idiot guy! He stupidly left the garage door open.

Revised Tweet: His name should be silly guy! He unwisely left the garage door open.

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

//Write a program that contains the following two methods Write a method that accepts a user string as a parameter and converts any words that are in all uppercase to all lowercase.

import java.util.Scanner;
import java.util.StringTokenizer;
import java.lang.String;

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

Scanner kb = new Scanner(System.in);
String str;
System.out.print("Enter a string: ");
while(!kb.hasNext("[A-Za-z]*[1-9]*")) //only accept sentance start with character and we can include in between numeric value also
{
System.out.println("String can only start with alphabetic Character");
kb.nextLine();
}
str = kb.nextLine();
System.out.println("Current:"+ str);
System.out.print("Revised text after coverting to Uppercase:");
System.out.println(cap(str));

System.out.println(rep(str));

}

// Method 1:That accepts a user string as a parameter and converts any words that are in all uppercase to all lowercase.

public static String cap(String str){
char[] arr = str.toCharArray();
// Start off by indicating to capitalize the first letter.
boolean cap = true;

for (int i = 0; i<arr.length; i++){
if (cap == true && !Character.isWhitespace(arr[i])) {
arr[i] = Character.toUpperCase(arr[i]);
cap = false;
}

else {
if (arr[i] == '.' || arr[i] == '?') {
cap = true;
}
}
}
String s = new String(arr);
return s;
}

//Method 2: That accepts a user string and returns the converted string. Certain (target) words are replaced with replacement words

public static String rep(String str)

{

String Str1 = str.replaceAll("tbh/TBH/Tbh", "To be honest");
Str1 = str.replaceAll("idiot", "silly");
Str1 = str.replaceAll("stupid", "unwise");
Str1 = str.replaceAll("stupidly", "unwisely");
Str1 = str.replaceAll("yo","hey");
Str1 = str.replaceAll("fake", "innovative");
Str1 = str.replaceAll("dope", "exciting");

return Str1;

}
}

);
String str;
System.out.print("Enter a string: ");
while (!kb.hasNext("[A-Za-z]*[1-9]*")) {
System.out.println("String can only start with alphabetic Character");
kb.nextLine();
}
str = kb.nextLine();
System.out.println(str);
System.out.println(cap(str));

System.out.println(rep(str));

}
  
public static String cap(String str){
char[] arr = str.toCharArray();

// Start off by indicating to capitalize the first letter.
boolean cap = true;
  
for (int i = 0; i<arr.length; i++){
if (cap == true && !Character.isWhitespace(arr[i])) {
arr[i] = Character.toUpperCase(arr[i]);
cap = false;
}
  
else {
if (arr[i] == '.' || arr[i] == '?') {
cap = true;
}
}
}
String s = new String(arr);
return s;
}

public static String rep(String str)

{

String Str1 = str.replaceAll("tbh/TBH/Tbh", "To be honest");
Str1 = str.replaceAll("idiot", "silly");
Str1 = str.replaceAll("stupid", "unwise");
Str1 = str.replaceAll("stupidly", "unwisely");
Str1 = str.replaceAll("yo", "hey");
Str1 = str.replaceAll("fake", "innovative");
Str1 = str.replaceAll("dope", "exciting");

//System.out.println(Str1);

return Str1

}
}

Add a comment
Know the answer?
Add Answer to:
JAVA PROGRAM Write a program that contains the following two methods Write a method that accepts...
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
  • Complete the printPalindrome method in the provided Palindrome.java program. The printPalindrome method accepts a String as...

    Complete the printPalindrome method in the provided Palindrome.java program. The printPalindrome method accepts a String as a parameter and prints whether the parameter String is a palindrome (i.e., reads the same forwards as it does backwards, for example, "abba" or "racecar"). Make the code case-insensitive, so that words like "Abba" and "Madam" will be considered palindromes. import java.util.*; /** * Determines if a user entered String is a palindrome, which means the String * is the same forward and reverse....

  • 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...

  • 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...

  • ***LOOPS NOT ALLOWED*** String variables and use of String methods: Write a complete Java program which...

    ***LOOPS NOT ALLOWED*** String variables and use of String methods: Write a complete Java program which prompts the user for a string with 3 words each separated by a single space and reads the line into one String variable using nextline(). Extract each word and add them to a new String variable in reverse order with a space between the words. Have the 1st letter of the new string in upper case and all other strings in lower case letter....

  • In java, write a program with a recursive method which asks the user for a text...

    In java, write a program with a recursive method which asks the user for a text file (verifying that the text file exists and is readable) and opens the file and for each word in the file determines if the word only contains characters and determines if the word is alpha opposite. Now what I mean by alpha opposite is that each letter is the word is opposite from the other letter in the alphabet. For example take the word...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • Submit Chapter6.java with a public static method named justifyText that accepts two parameters; a Scanner representing...

    Submit Chapter6.java with a public static method named justifyText that accepts two parameters; a Scanner representing a file as the first parameter, and an int width specifying the output text width. Your method writes the text file contents to the console in full justification form (I'm sure you've seen this in Microsoft Word full.docx ).  For example, if a Scanner is reading an input file containing the following text: Four score and seven years ago our fathers brought forth on this...

  • Write a program named text_indexing.c that does the following: Reads text and stores it as one...

    Write a program named text_indexing.c that does the following: Reads text and stores it as one string called text. You can read from a file or from the user. (In my implementation, I read only one paragraph (up to new line) from the user. With this same code, I am able to read data from a file by using input redirection (executable < filename) when I run the program. See sample runs below). You can assume that the text will...

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