Question: What is wrong with the following code snippet? String [ ] data = { abc, def, ghi, jkl } ; String searchedValue =

What is wrong with the following code snippet?
String[] data ={ "abc", "def", "ghi", "jkl"};
String searchedValue = "ghi";
int pos =0;
boolean found = false;
while (pos < data.length)
{
if (data[pos].equals(searchedValue))
{
found = true;
}
else
{
found = false;
}
pos++;
}
if (found)
{
System.out.println("Found at position: "+ pos);
}
else
{
System.out.println("Not found");
}

Step by Step Solution

3.43 Rating (153 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The issue in the code is with the logic of setting the found flag inside the while loop The problem ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!