Write a program that reads a sentence using a dialog box. Depending on the lsat
ID: 3567065 • Letter: W
Question
Write a program that reads a sentence using a dialog box. Depending on the lsat character of the sentence, output another dialog box identifying the sentence as declarative, interrogative, exclamatory, or other.
This is what I have so far.
import javax.swing.JOptionPane;
public class Sentence
{
public static void main (String [] args)
{
char inputChar = JOptionPane.showInputDialog(null, "Please input a sentence").charAt(0);
switch (inputChar)
{
case '.': JOptionPane.showMessageDialog(null, "Your sentence is Declarative");
break;
case '?': JOptionPane.showMessageDialog(null, "Your sentence is Interrogative");
break;
case '!': JOptionPane.showMessageDialog(null, "Your sentence is Exclamatory");
break;
default: JOptionPane.showMessageDialog(null, "Your sentence is Other");
}
}
}
Explanation / Answer
You are almost right but you need to check the last character of the input string not the first character.
heres' the modified code, hope it helps
import javax.swing.JOptionPane;
public class Sentence {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Please input a sentence");
char inputChar = input.charAt(input.length() - 1);
String s = "Other";
switch (inputChar) {
case '.':
s = "Declarative";
break;
case '?':
s = "Interrogative";
break;
case '!':
s = "Exclamatory";
break;
}
JOptionPane.showMessageDialog(null, "Your sentence is " + s);
}
}