This is my answer. But it is incorrect. Could you help to revise
that ?
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.
Code in java:
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::
Another version of code in java where user is asked
to enter userInput string and firstLetter
character::
Code in java::
import java.util.Scanner;
public class CharMatching{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String userInput="";
char firstLetter='-';
System.out.println("nEnter the string as userInput::");
userInput=sc.nextLine();
System.out.println("nEnter the character as
firstLetter::");
firstLetter=sc.next().charAt(0);;
if(userInput.charAt(0)==firstLetter){
System.out.println("Found match: "+firstLetter);
}else{
System.out.println("No match:"+firstLetter);
}
return;
}
}
Output::
Test case 1::
Test case 2::
Test case 3::
Thank you!!