Write an expression in JAVA to detect that the first character of user Input matches first Letter.

Question
0

Answer ( 1 )

  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.

    Best answer

Leave an answer