Question

In: Computer Science

(Java Zybooks) Print "Censored" if userInput contains the word "darn", else print userInput. End with newline....

(Java Zybooks) Print "Censored" if userInput contains the word "darn", else print userInput. End with newline. Ex: If userInput is "That darn cat.", then output is:

Censored

Ex: If userInput is "Dang, that was scary!", then output is:

Dang, that was scary!

Note: If the submitted code has an out-of-range access, the system will stop running the code after a few seconds, and report "Program end never reached." The system doesn't print the test case that caused the reported message.

import java.util.Scanner;

public class CensoredWords {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
String userInput;

userInput = scnr.nextLine();

/* Your solution goes here */

}
}

Solutions

Expert Solution

Note: The modified part of the code is highlighted in grey and bold.

Screenshot of the code:

Sample Output:

Code to copy:

//Import required package.

import java.util.Scanner;

//Define the class CensoredWords.

public class CensoredWords

{

//Start the main() method.

public static void main (String [] args)

{

    //Create an object of the class Scanner class.

    Scanner scnr = new Scanner(System.in);

    //Declare required variables.

    String userInput;

    //Prompt the user to input a string.

    userInput = scnr.nextLine();

    /* Your solution goes here */

    //Find the index of string darn in the string

    //userInput using the method indexOf(). If the

    //returned value is not -1, then it means that

    //the word darn is included in the string and

    //display censored.

    if(userInput.indexOf("darn") != -1)

    {

      System.out.println("Censored");

    }

   

    //Otherwise, display the string userInput with

    //newline.

    else

    {

      System.out.println(userInput);

    }

}

}


Related Solutions

Print "Censored" if userInput contains the word "darn", else print userInput. End with newline. Note: These...
Print "Censored" if userInput contains the word "darn", else print userInput. End with newline. Note: These activities may test code with different test values. This activity will perform three tests, with userInput of "That darn cat.", then with "Dang, that was scary!", then with "I'm darning your socks.". See "How to Use zyBooks". . Also note: If the submitted code has an out-of-range access, the system will stop running the code after a few seconds, and report "Program end never...
Print "Censored" if userInput contains the word "darn", else print userInput. End with newline. Ex: If...
Print "Censored" if userInput contains the word "darn", else print userInput. End with newline. Ex: If userInput is "That darn cat.", then output is: Censored Ex: If userInput is "Dang, that was scary!", then output is: Dang, that was scary! Note: If the submitted code has an out-of-range access, the system will stop running the code after a few seconds, and report "Program end never reached." The system doesn't print the test case that caused the reported message. #include <iostream>...
Print "Censored" if userInput contains "darn", else print iserInput. End with newline. Ex: If userInput is...
Print "Censored" if userInput contains "darn", else print iserInput. End with newline. Ex: If userInput is "That darn cat", then output is:
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print the array double the value in the array at position start recMethod(array, start+1, end) print the array } else { print "done" } } Assume the method is invoked with array = [3, 4, 6, 7, 8, 10, 4], start = 2, and end = 5 1.How many times is the array printed before the word "done" is printed? 2.How many times is the...
Write a JAVA program that contains the following 4 void methods whic will print all subtrings...
Write a JAVA program that contains the following 4 void methods whic will print all subtrings of s in specific orders: printSub1(String s), printSub2(String s), printSub3(String s), and printSub4(String s) For example, if S = "abcd": printSub1 will print "a", "ab", "abc", "abcd", "b", "bc", "bcd", "c", "cd", "d" printSub2 will print "a", "ab", "b", "abc", "bc", "c", "abcd", "bcd", "cd", "d" printSub3 will print "d", "c", "b", "a", "cd", "bc", "ab", "bcd", "abc", "abcd" printSub4 will print "abcd", "bcd",...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT