Question

Write a program that takes a String containing a text using the method signature String useProperGrammar(String...

Write a program that takes a String containing a text using the method signature

String useProperGrammar(String text)

Your method should replace the word ‘2’ with ‘to’ and return the updated text.

For example,

useProperGrammar("can you go 2 the store?")

should return

"can you go to the store?"

This method should also print out the number of grammatical errors that were fixed.

For example, for useProperGrammar("back 2 back 2 back"), the method would also print:

Fixed 2 grammatical errors:

In the main method, ask the user to input a String, and print the results of useProperGrammar using the user input.

Coding Portion:

public class Grammar
{
public static void main(String[] args)
{
// Ask the user to enter a sentence that uses the word 2 instead of to.
  
// Call the method useProperGrammar to process the string according to
// the directions.
}
  
public static String useProperGrammar(String theText)
{
// Process the sentence that is sent to this method and replace every 2
// with the word to.
}
}

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

CODE:

package grammar;

//importing Scanner class
import java.util.Scanner;
public class Grammar {
public static void main(String[] args) {
  
//creating object for Scanner class
Scanner scan = new Scanner(System.in);
  
System.out.print("Enter the text with 2 to be changed : ");
//read a line of text as input
String theText= scan.nextLine();   
  
//calling useProperGrammar() method with theText as parameter.
String newText = useProperGrammar(theText);
//printing the modified text returned by the useProperGrammar() method
System.out.println("Modified text : "+newText);
  
}
//useProperGrammar() method accpets a line of text as input and returns a modified line of text   
public static String useProperGrammar(String theText){
  
//initializing s and count as 0
int s = 0;
int count = 0; //count stores the number of occurence of '2' in the String
//creating an empty String , at which we will append a number of substrings to get the required String
String newText = "";
  
//loop runs for the length of the input String
for(int i = 0 ; i < theText.length() ; i++){
if(theText.charAt(i) == '2'){ //if statement works, when the character equals to '2'
//taking a section of String from s to i and appending them to the new String with '2' being replaced by 'to'
newText += theText.substring(s, i) + "to";
//updating s as 1+1   
s = i+1 ;
//incrementing count
count++;
}
}
  
System.out.println("Fixed "+count+" grammatical error(s).");
//appending the remaining characters of the main(input) String to the new String
newText += theText.substring(s, theText.length());
return newText;
}
}

//Screenshot of Code:

//Sample I/O:

//Hope this helps.

//Give Thumbs up.

//Thank you.

//Comment if you got any doubt.

Add a comment
Know the answer?
Add Answer to:
Write a program that takes a String containing a text using the method signature String useProperGrammar(String...
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
  • A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g.,...

    A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. In this program, ask the user to input some text and print out whether or not that text is a palindrome. Create the Boolean method isPalindrome which determines if a String is a palindrome, which means it is the same forwards and backwards. It should return a boolean of whether or not it was a palindrome. Create the method reverse...

  • Write a JAVA program that has a method which accepts a string and prints it back...

    Write a JAVA program that has a method which accepts a string and prints it back to the user with all vowels replaced with *'s (multiple asterisks not '*s') with a new line after the string. The method signature should look like this: public static void replacePrint(String input)

  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

  • (USING THE PROGRAM MARS) Using the MemoryAccess program we wrote in class as a starting point,...

    (USING THE PROGRAM MARS) Using the MemoryAccess program we wrote in class as a starting point, write a program called LoadStore # Title : Memory access.asm #Desc: Practice initially memory, #in val1 = 0x0a; #int val2 = 0x0b; #int result; #string resultstring = " final answer : "; #string returnchar = "\n"; #void main() { #   result = val1 + val2; #   cout<<< resultstring << returnchar; #} .data val1: .word 0x0a   #store 0xa into variable val1 val2: .word 0x0b   #store...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • Write the method: public static String doubleVowels(String x). This method takes a String x and returns...

    Write the method: public static String doubleVowels(String x). This method takes a String x and returns a new String y which is identical to x except wherever there is a vowel in x, there will be two of that same vowel in the returned String y. The original String x will contain only lower case letters. For example, doubleVowels("easy") should return the String "eeaasy". Another example: doubleVowels("abootstrap") should return the String "aabooootstraap". Another example: doubleVowels("gggrrrhh") should return the String "gggrrrhh"....

  • package week_3; /** Write a method called countUppercase that takes a String array argument. You can...

    package week_3; /** Write a method called countUppercase that takes a String array argument. You can assume that every element in the array is a one-letter String, for example String[] test = { "a", "B", "c", "D", "e"}; This method will count the number of uppercase letters from the set A through Z in the array, and return that number. So for the example array above, your method will return 2. You will need to use some Java library methods....

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

  • Write a static method called encodeString that takes a string as input and prints to the...

    Write a static method called encodeString that takes a string as input and prints to the console a word where each letter is advanced by one value. In addition, your method should return the encoded String. You may make use of the method provided below to help you. public static char encode(char x) { int current = x; current++; return (char)current; } This method takes a character and returns an "encoded" character. In other words, encode('a') will return 'b'. Your...

  • Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a...

    Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a method that computes and returns the area of a square using the following header: public static double area ( double side) Write a main method that prompts the user to enter the side of a square, calls the area method then displays the area. Question 1a: Write a method that converts miles to kilometers using the following header: public static double convertToKilometers (double miles)...

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
ADVERTISEMENT