+ 3
26th Oct 2019, 4:53 PM
Sarah Joudeh
Sarah Joudeh - avatar
5 Answers
+ 7
Change your for loop to for(int i=0; i<myarray.length; i++) { //... } Also you shouldn't compare Strings with the equals operator == but with String's equal method.
26th Oct 2019, 6:57 PM
Tashi N
Tashi N - avatar
+ 2
Nicki Sky Please move the code link from question title into the Description section. Links don't work in title nor Relevant Tags section, they only work in Description section. (Edit) Thanks for understanding @Nicki Sky
26th Oct 2019, 6:34 PM
Ipang
+ 1
int i; for (i=0; i<myarray.length && ! A.equals( myarray[i] ); i++); if ( i !=myarray.length) System.out.println("found"); else System.out.println("not found");
26th Oct 2019, 7:23 PM
zemiak
0
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("please enter a name"); String A =scan.nextLine(); String myarray []= { "muna" ,"sarah","yousef" ,"noor", "ahmed", "jumana"} ; for(int i =0; A <myarray.length;i++) { if(A==myarray[i]) System.out.println("found"); break; else System.out.println("not found"); } } }
26th Oct 2019, 9:19 PM
Phineas
0
You should edit condition in for loop for (int i=0;i <myarray.length; i++) To go through all element in array As you you know array.length return size of an array Plus for comparing two string (or any object ) you should use method equals() Because String is an object and can't compared like basic data such as int or float numbers. So write if (A.equals(myarray[i])) System.out.println("found")
27th Oct 2019, 1:28 PM
Mohammad Reza Rezaei Barzani
Mohammad Reza Rezaei Barzani - avatar