187k views
3 votes
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 (/* Your solution goes here */) { System.out.println("Found match: " + firstLetter); } else { System.out.println("No match: " + firstLetter); } return; } }

User Kotauskas
by
4.0k points

1 Answer

3 votes

Answer: Provided in the explanation segment

Step-by-step explanation:

Below is the code to run this program.

we have that the

Program

import java.util.Scanner;

public class CharMatching {

public static void main(String[] args) {

//Scanner object for keyboard read

Scanner scnr=new Scanner(System.in);

//Variable for user input string

String userInput;

//Variable for firstletter input

char firstLetter;

//Read user input string

userInput=scnr.nextLine();

//Read first letter from user

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

//Comparison without case sensititvity and result

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

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

}

else {

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

}

}

}

Output

Hello

h

Found match: h

cheers i hope this helped !!!

User Skyllet
by
3.8k points