Write an expression to detect that the first character of user input matches first letter.

@@@ < >肛 learn.zybooks.com ○山。 Lab 2 Post Test: COMPUTER Computer Science question.. 3.8. String access operations geval关于初습化boolean( Chegg Study I Gulded Sout >EI Lean.zybooks.... java怎么从string里拿第一个 zyBooks Library > CSCE 1 55A home-3.8: String access operations @ Help/ FAQ θ Bojun Zhong ▼ ACTIVITY3.8.1: Looking for characters. ☆个人收藏 IMG 0264.JPG 4-5学生选课 添加课程11 (0 F22BB1 79-E979-46F2-AA09 20E74856-C89B-4889-85A... Write an expression to detect that the first character of userinput matches firstLetter canner; 3 public class CharMatching 4 public static void main (String args) [ String userInput- char firstLetter = -; userInputbanana firstLetter = b; 10 if (userInput.charAt(e)b) System.out.printlnFound match: firstLetter); 12 13 14 15 16 17 18 else System.out.println(No match: firstletter); return; Testing:banana, b Your output Found match: b ﹀ Testing:·abacus, c. Your output No match: c

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::

CWINDOWSlsystem32cmd.exe C:CheggJava>iavac Ch C: CheggJava>java CharMatching arMatching.java C: CheggJava>

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::

CAWINDOWS1system321cmd.exe C: CheggJava>javac CharMatching.java :CheggJava>java CharMatching Enter the string as userInput:

Test case 2::

WINDOWS system321cmd.exe :CheggJava>javac CharMatching.java :CheggJava>java CharMatching Enter the string as userInput:: ab

Test case 3::

CAWINDOWS1system321cmd.exe C: CheggJava>javac CharMatching.java :CheggJava>java CharMatching Enter the string as userInput:

Thank you!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts