import java.util.Scanner;//import Scanner
public class review{ //name of the program
public static void main(String[]args){ // main statement
Scanner i=new Scanner(System.in);
System.out.println("Enter a string");
String b=i.nextLine();
System.out.println("Enter a letter");
char c=i.next().charAt(0);
System.out.println("Answer is "+test1(b,c));
}
public static boolean test1(String a, char b){
boolean result= true;
for(int i=0;i<a.length();i++)
if(a.charAt(i)==b)
result =true;
else
result=false;
return result;
}
}
this program is looking for checking the char is in the string or not.
Hello, E = true
Hello, a = false
In this method
test1, yourforloop will traverse the whole line although it finds theletterinto thestring. so update it like this:Because, if the
letteris found into thestringyou don't need to check further, hence:Note that,
returnstatement causes execution to leave the currentfunctionor tersely current subordinate.