Search a String in a 2D array
import javax.swing.JOptionPane;
public class Search_str_arr
{
public static void main(String[] args)
{
String arr_String[][]=new String[3][3];
for (int i=0 ;i<=2 ;i++)
{
for (int j=0;j<=2;j++ )
{
arr_String[i][j]=JOptionPane.showInputDialog("Enter string for arr_String["+i+"]["+j+"]");
}
}
String sData=JOptionPane.showInputDialog("Enter Data to be searched");
Boolean bFlag=false;
for (int i=0 ;i<=2 ;i++)
{
for (int j=0;j<=2;j++ )
{
if (arr_String[i][j]==sData)
{
bFlag=true;
}
}
if (bFlag=true)
{
break ;
}
}
if (bFlag=true)
{
System.out.println("the data exists");
}
else
{
System.out.println("the data does not exist");
}
}
}
No comments:
Post a Comment