(Java coding) I need to create a program that takes a sentence as an input (one line...
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 ends with a question mark '?' and the sentence contains an even number of characters, then print "That's an even question" on the console.
- If the sentence ends with a question mark '?' and the sentence contains an odd number of characters, then print "That's an odd question".
- If the sentence ends with an exclamation mark '!', print the word Wow.
- If the sentence ends with a comma output "More to come!"
- 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; }
}
}
Microsoft Visual C# An Introduction to Object-Oriented Programming
ISBN: 978-1337102100
7th edition
Authors: Joyce Farrell