53.0k views
4 votes
Please code this in java

Given string userInput, output "A question" if the string's last character is '?'. Otherwise, output "Not a question". End with a newline.

Ex: If the input is:

How are you?

then the output is:

A question

given:
import java.util.Scanner;

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

userInput = scnr.nextLine();

/* Your code goes here */

}
}

1 Answer

4 votes

Answer:

Step-by-step explanation:

import java.util.Scanner;

public class StringCompare {

public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);

String userInput;

userInput = scnr.nextLine();

if (userInput.endsWith("?")) {

System.out.println("A question");

} else {

System.out.println("Not a question");

}

}

}

In this code, we use the endsWith() method to check if the last character of the userInput string is a question mark. Based on the result, we output either "A question" or "Not a question".

Make sure to save the program with the filename "StringCompare.java" and compile and run it using a Java compiler or IDE.

User Ffriend
by
7.8k points