Help in Arrays


i want to show the element found and also it should show the position of the element, e.g. from the array having elements 1,2,3,4,5,6,7,8,9,0 and if element 6 is found then it show output as "Element Found = 6 at position 7 in array"

import javax.swing.*;
1.public class SearchingFromArray
2.{
3. public static void main(String[] args)
4. {
5. int a[]=new int[10];
6. int i,x,flag;
7. String st;
8. for(i=0;i<10;i++)
9. {
10. st=JOptionPane.showInputDialog("Enter Elements");
11. a[i]=Integer.parseInt(st);
12. }
13. st=JOptionPane.showInputDialog("Enter The Elements To Be Searched");
14. x=Integer.parseInt(st);
15. flag=0;
16. for(i=0;i<10;i++)
17. {
18. if (a[i]==x)
19. flag=1;
20. }
21. if (flag==1)
22. {
23. JOptionPane.showMessageDialog(null,"Elements Found in an Array = " +x+ " at Position " + a[i]);
24. }
25. else
26. JOptionPane.showMessageDialog(null,"Not Found");
27. }
28.}
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories