Question: (Java coding) I need to create a program that takes a sentence as an input (one line sentence) and returns the following If the sentence

(Java coding)

I need to create a program that takes a sentence as an input (one line sentence) and returns the following

  1. If the sentence ends with a question mark '?' and the sentence contains an even number of characters, then print "That's an even question" on the console.
  2. If the sentence ends with a question mark '?' and the sentence contains an odd number of characters, then print "That's an odd question".
  3. If the sentence ends with an exclamation mark '!', print the word Wow.
  4. If the sentence ends with a comma output "More to come!"
  5. In all other cases, your program will output the string You always say followed by the sentence entered by the user.

Keep in mind we have to use a switch statement, and again this is for JAVA. Please look below for my current code. I'm not sure what exactly I'm doing wrong but I am returning errors so I know I'm doing something incorrectly. I'm stuck. I never finished because I did the switch statement correctly (I think?) but I'm having issues returning how exactly to get to those cases. Please assist. Since I have x = 0 right now, it always returns my default case. if i change it it changes ; however, how do I make it so it will use the cases in the switch statement? Like, for example, if we take the even question, even if i write an if statement saying if the last character is a ? and it is even set x = 1 it still doesn't change x since x is initialized as 0 Heres my current code :

package activity2;

import java.util.Scanner;

public class SentenceProcessor {

public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Provide a one-line sentence : "); String userInput = keyboard.nextLine(); int n = userInput.length(); int x = 0; char last = userInput.charAt(n-1); if(userInput.endsWith("?") && userInput.length()%2==0) { x = 1; }

switch (x) { case 1: System.out.println("That's an even question."); break; case 2: System.out.println("That's an odd question."); break; case 3: System.out.println("Wow."); break; case 4: System.out.println("More to come!"); break; default: System.out.println("You always say, "); break; }

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!