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
, yourfor
loop will traverse the whole line although it finds theletter
into thestring
. so update it like this:Because, if the
letter
is found into thestring
you don't need to check further, hence:Note that,
return
statement causes execution to leave the currentfunction
or tersely current subordinate.