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

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


Answer

Explanation:

- As in the provided code we do not prompt user to enter string and charater, instead we directly hard code and initialize the variables userInput and firstLetter in code itself.

- So when I did so I got correct output.

- The only change I made is in if statement.

- See the code provided 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;
}
}

Output:

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

Leave a Reply

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

Related Posts