Complete Question
Write an expression to detect that the first character of userInput matches firstletter
import java.util.Scanner;
public class CharMatching
{
public static void main (String [] args)
{
Scanner scnr = new Scanner (System.in);
string userInput;
char firstLetter;
userInput = scnr.nextLine();
firstLetter = scnr.nextLine().charAt(0);
if ( = firstLetter ) {// Your solution goes here
System.out.println("Found match: " + firstLetter); }
else { System.out.println("No match: " + firstLetter); }
return;
}
}
Answer:
Replace if( = firstLetter) with the following
if(userInput.CharAt(0) == firstLetter)
What the above code segment does is that it extracts the first character of userInput and compare it with the content of firstLetter
The full program becomes
import java.util.Scanner;
public class CharMatching
{
public static void main (String [] args)
{
Scanner scnr = new Scanner (System.in);
string userInput;
char firstLetter;
userInput = scnr.nextLine();
firstLetter = scnr.nextLine().charAt(0);
if(userInput.CharAt(0) == firstLetter){
System.out.println("Found match: " + firstLetter); }
else { System.out.println("No match: " + firstLetter); }
return;
}
}
Explanation: