Write an expression in JAVA to detect that the first character of user Input matches first Letter.
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.Morbi adipiscing gravdio, sit amet suscipit risus ultrices eu.Fusce viverra neque at purus laoreet consequa.Vivamus vulputate posuere nisl quis consequat.
Answer ( 1 )
import java.util.Scanner;
public class CharMatching {
public static void main (String [] args) {
String userInput = “”;
char firstLetter = ‘-‘;
userInput = “banana”;
firstLetter = ‘b’;
if (/* Your solution goes here */) {
System.out.println(“Found match: ” + firstLetter);
}
else {
System.out.println(“No match: ” + firstLetter);
}
return;
}
}
Arriving at Characters of a String in JAVA:
JAVA String class has a predefined capacity named charAt(int) which can be utilized to access characters of a string giving their area. Be that as it may, we are not permitted to change a character of a String object since they are permanent.
Answer and Explanation:
We can arrive at any character of a given string utilizing charAt(int) strategy by passing the ideal area beginning at 0.