+ 3
How can I make this code work!!!
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.
+ 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
+ 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");
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");
}
}
}
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")