Write an expression to detect that the first character of userinput matches firstLetter. 1 import java.util.Scanner; 3 public class CharMatching 4 public static void main (String [] args) { Scanner scnr new Scanner(System.in); String userInput; char firstLetter; userInput = scnr.nextLine(); firstLetter scnr.nextLine().charAt (0); 12 13 14 15 if (/* Your solution goes here * System.out.printin( "Found match: "firstLetter); else t System.out.println("No match: " firstLetter); 17 18 19 20 21 return

Respuesta :

Answer:

if(Character.toUpperCase(firstLetter)==Character.toUpperCase(userInput.charAt(0)))

Explanation:

Ver imagen DarthVaderBarbie

The given source code is an illustration of characters and strings in Java program.

To complete the code segment, the comment /* Your solution goes here */ in the program, should be replaced with any of the following expressions:

  • Character.toUpperCase(firstLetter)==Character.toUpperCase(userInput.charAt(0))
  • Character.toLowerCase(firstLetter)==Character.toLowerCase(userInput.charAt(0))

Using the first instruction above

  • Variable firstLetter will first be converted to upper case
  • The first character of userInput will then be extracted using charAt(0)
  • The first character is then converted to upper case
  • Lastly, both characters are compared

So, the complete code (where comments are used to explain each line) is as follows:

//This imports the Scanner library

import java.util.Scanner;

//This defines the program class

public class Main {

//This defines the program method

public static void main (String [] args) {

//This creates a Scanner object

Scanner scnr = new Scanner(System.in);

//This declares variables userInput and firstLetter

String userInput; char firstLetter;

//This gets input for userInput

userInput = scnr.nextLine();

//This gets input for firstLetter

firstLetter = scnr.nextLine().charAt(0);

//This checks if the first letter of userInput matches with variable firstLetter

if (Character.toUpperCase(firstLetter)==Character.toUpperCase(userInput.charAt(0))){

//If yes, found match is printed

System.out.println( "Found match: "+firstLetter); }

else{

//If otherwise, no match is printed

System.out.println("No match: "+ firstLetter);

}

}

}//Program ends here

See attachment for the complete code and the sample run

Read more about java programs at:

https://brainly.com/question/2266606

Ver imagen MrRoyal