215k views
4 votes
Please help me code this in java

Given string inputStr on one line, character inputChar on a second line, and integer strIndex on a third line, output "Found match" if the character at index strIndex of inputStr matches inputChar. Otherwise, output "No match". End with a newline.

Ex: If the input is:

weigh
h
4
then the output is: Found match
Note: Assume the length of string inputStr is greater than strIndex.

Given:
import java.util.Scanner;

public class CharMatching {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String inputStr;
char inputChar;
int strIndex;

inputStr = scnr.nextLine();
inputChar = scnr.next().charAt(0);
strIndex = scnr.nextInt();

User Jabari
by
7.9k points

1 Answer

4 votes

Answer:

Hope this helps

Step-by-step explanation:

import java.util.Scanner;

public class CharMatching {

public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);

String inputStr;

char inputChar;

int strIndex;

inputStr = scnr.nextLine();

inputChar = scnr.next().charAt(0);

strIndex = scnr.nextInt();

if (inputStr.charAt(strIndex) == inputChar) {

System.out.println("Found match");

} else {

System.out.println("No match");

}

scnr.close();

}

}