Write an expression to detect that the first character of userinput matches firstletter.

Write an expression to detect that the first character of user input matches the first letter.


Answer

Explanation:- As in the given code, we are not asking the user to input a string and a character, but we are directly coding and initializing the userInput and firstLetter variables in the code itself. – So when I did that, I got the correct output. – The only change I made was in the if statement. – See the code below.

import java.util.Scanner;
public class CharMatching{
public static void main(String[] args){
String userInput="";
char firstLetter='-';
userInput="1,2,Buckl my shoe.";
firstLetter='1';
/*
* In if condition below I have changed from 'b' to variable name
* firstLetter that stores first character to be checked.
*/
if(userInput.charAt(0)==firstLetter){
System.out.println("Found match: "+firstLetter);
}else{
System.out.println("No match:"+firstLetter);
}
return;
}
}

Results:

Write an expression to detect that the first character of user input matches the first letter

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *