How to find first character in a string in java


Asked by admin @ in Computers and Technology viewed by 352 People


Write an expression to detect that the first character of userinput matches firstLetter. 1 import java.util.Scanner; 3 public class CharMatching 4 public static void main (String [] args) { Scanner scnr new Scanner(System.in); String userInput; char firstLetter; userInput = scnr.nextLine(); firstLetter scnr.nextLine().charAt (0); 12 13 14 15 if (/* Your solution goes here * System.out.printin( "Found match: "firstLetter); else t System.out.println("No match: " firstLetter); 17 18 19 20 21 return

Answered by admin @



The given source code is an illustration of characters and strings in Java program.

To complete the code segment, the comment /* Your solution goes here */ in the program, should be replaced with any of the following expressions:

  • Character.toUpperCase(firstLetter)==Character.toUpperCase(userInput.charAt(0))
  • Character.toLowerCase(firstLetter)==Character.toLowerCase(userInput.charAt(0))

Using the first instruction above

  • Variable firstLetter will first be converted to upper case
  • The first character of userInput will then be extracted using charAt(0)
  • The first character is then converted to upper case
  • Lastly, both characters are compared

So, the complete code (where comments are used to explain each line) is as follows:

//This imports the Scanner library

import java.util.Scanner;

//This defines the program class

public class Main {

//This defines the program method

public static void main (String [] args) {

//This creates a Scanner object

Scanner scnr = new Scanner(System.in);

//This declares variables userInput and firstLetter

String userInput; char firstLetter;

//This gets input for userInput

userInput = scnr.nextLine();

//This gets input for firstLetter

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

//This checks if the first letter of userInput matches with variable firstLetter

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

//If yes, found match is printed

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

else{

//If otherwise, no match is printed

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

}

}

}//Program ends here

See attachment for the complete code and the sample run

Read more about java programs at:

brainly.com/question/2266606


Similar Questions

How to find the first quartile of a data set

Asked by admin @ in Mathematics viewed by 265 persons

What is the first quartile of this data set

How to find the first term of an arithmetic sequence

Asked by admin @ in Mathematics viewed by 329 persons

The third term of an arithmetic sequence is equal to 9 and the sum of the first 8 term is 42. Find the first term and the common difference

Find the first three terms of each arithmetic series calculator

Asked by admin @ in Mathematics viewed by 320 persons

Write the first five terms of a sequence. Write both an explicit formula and a recursive formula for a general term in the sequence. recursive formula for arithmetic sequence recursive …

Find the mean of first 5 prime numbers

Asked by admin @ in Math viewed by 347 persons

Find the arithmetic mean of first five prime numbers​

Find the sum of first 20 natural numbers

Asked by admin @ in Math viewed by 338 persons

What is the sum of the first 20 natural numbers?

Find the mean of first 10 prime numbers

Asked by admin @ in Math viewed by 321 persons

Find the mean of first ten prime numbers

Find the median of first 10 even numbers

Asked by admin @ in Math viewed by 371 persons

Find the median of first 10 even number

Find the mean of first n natural numbers

Asked by admin @ in Math viewed by 459 persons

Mean of first n natural numbers

Find the average of first 40 natural numbers

Asked by admin @ in Math viewed by 333 persons

Find the average of first 40 natural numbers

Find the median of first 10 prime numbers

Asked by admin @ in Math viewed by 312 persons

Find the median of first ten prime numbers?

Find the sum of first 1000 positive integers

Asked by admin @ in Math viewed by 305 persons

Find the sum of first 1000 positive integers

Find the sum of first 10 natural numbers

Asked by admin @ in Math viewed by 341 persons

Find the sum of first 10 natural number.

Find the mean of first 10 natural numbers

Asked by admin @ in Math viewed by 351 persons

What is the mean of first 10 natural numbers?

Find the sum of first 100 natural numbers

Asked by admin @ in Math viewed by 327 persons

Find the sum of first 100 natural numbers.

Find the mean of first five natural numbers

Asked by admin @ in Math viewed by 353 persons

Find the mean of first 5 even natural number

Most viewed questions in Computers And Technology


A customer wants to increase the storage capacity by 25gb

Asked by admin @ in Computers and Technology viewed by 4435 persons




Solid yellow line bordering your lane of traffic marks a

Asked by admin @ in Computers and Technology viewed by 610 persons



Which of the following is not a windows utility program

Asked by admin @ in Computers and Technology viewed by 599 persons


When is a wrecker considered to be an emergency vehicle

Asked by admin @ in Computers and Technology viewed by 579 persons


Which operation on a pwc requires more than idle speed

Asked by admin @ in Computers and Technology viewed by 576 persons



Which of the following statements is true of a database

Asked by admin @ in Computers and Technology viewed by 568 persons


Which of the following is not true about an entrepreneur

Asked by admin @ in Computers and Technology viewed by 540 persons


Kitchen gadgets draw a context diagram for the order system

Asked by admin @ in Computers and Technology viewed by 518 persons