Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class SearchQuery {
  5.  
  6. static int count=0;
  7.  
  8. static char[] alpha= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  9.  
  10. static int size;
  11.  
  12. static int[] x = { -1, -1, -1, 0, 0, 1, 1, 1 };
  13. static int[] y = { -1, 0, 1, -1, 1, -1, 0, 1 };
  14.  
  15. public static void main(String[] args)
  16. {
  17. Scanner input=new Scanner(System.in);
  18.  
  19. System.out.print("What is the size of the Array?: ");
  20. size=input.nextInt();
  21.  
  22. char[][] matrix=new char[size][size];
  23.  
  24. print2D(matrix,size);
  25. System.out.println("Enter to query to search: ");
  26. String query=input.nextLine();
  27. query=input.nextLine();
  28. searchHorz(matrix,size,query);
  29. searchVert(matrix,size,query);
  30.  
  31. System.out.println(count);
  32.  
  33. }
  34.  
  35. public static char[][] print2D(char[][] array,int rows)
  36. {
  37.  
  38. for(int i=0;i<rows;i++)
  39. {
  40. for(int j=0;j<rows;j++)
  41. {
  42. int random=(int)(Math.random()*26);
  43. array[i][j]=alpha[random];
  44. System.out.print(array[i][j]);
  45. }
  46. System.out.println();
  47. }
  48. return array;
  49. }
  50.  
  51. public static int searchHorz(char[][] array,int rows,String word)
  52. {
  53.  
  54. for(int i=0;i<rows;i++)
  55. {
  56. for(int j=0;j<rows;j++)
  57. {
  58. if(array[i][j]==word.charAt(0))
  59. {
  60. count++;
  61. }
  62. }
  63. }
  64. return count;
  65. }
  66.  
  67. public static int searchVert(char[][] array,int rows,String word)
  68. {
  69. for(int i=0;i<rows;i++)
  70. {
  71. for(int j=0;j<rows;j++)
  72. {
  73. if(array[j][i]==word.charAt(0))
  74. {
  75. count++;
  76. }
  77. }
  78. }
  79. return count;
  80.  
  81. }
  82.  
  83. public static int searchDiag(char[][] array,int rows,String word)
  84. {
  85. return count;
  86. }
  87.  
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement