Guest User

Untitled

a guest
Aug 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. Java Anagrams reading from dictionary file
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Scanner;
  9.  
  10. public class Anagram3
  11. {
  12. static int size;
  13. static int count;
  14. static char[] charArray;
  15. static char[] words;
  16.  
  17. public static void main(String[] args) throws IOException
  18. {
  19. Scanner sc = new Scanner(System.in);
  20. System.out.println("Type the path of the dictionary to read from : ");
  21. String fileName = sc.nextLine();
  22.  
  23. List<String> dictionary = new ArrayList<String>();
  24.  
  25. BufferedReader br = null;
  26.  
  27. try
  28. {
  29. br = new BufferedReader(new FileReader(fileName));
  30. String word;
  31.  
  32. while((word = br.readLine())!=null)
  33. {
  34. dictionary.add(word);
  35. }
  36.  
  37. }
  38. catch(IOException e)
  39. {
  40. e.printStackTrace();
  41. }
  42. String[] words = new String[dictionary.size()];
  43. dictionary.toArray(words);
  44.  
  45. //for( int i = 0; i < words.length; i++ )
  46. // System.out.println(words[i]);
  47.  
  48.  
  49. System.out.println("nEnter the phrase to scramble: ");
  50. String input = sc.nextLine();
  51. System.out.println();
  52.  
  53.  
  54. size = input.length();
  55. count = 0;
  56. charArray = new char[size];
  57. for (int j = 0; j < size; j++)
  58. charArray[j] = input.charAt(j);
  59. doAnagram(size);
  60. }
  61.  
  62. public static void doAnagram(int newSize)
  63. {
  64. int limit;
  65. if (newSize == 1) // if too small, return;
  66. return;
  67. // for each position,
  68. for (int i = 0; i < newSize; i++) {
  69. doAnagram(newSize - 1); // anagram remaining
  70. if (newSize == 2) // if innermost,
  71. printAnagrams();
  72. rotate(newSize); // rotate word
  73. }
  74. }
  75.  
  76.  
  77. public static void rotate(int newSize)
  78. {
  79. int i;
  80. int position = size - newSize;
  81.  
  82. char temp = charArray[position];
  83.  
  84. for (i = position + 1; i < size; i++)
  85. charArray[i - 1] = charArray[i];
  86.  
  87. charArray[i - 1] = temp;
  88. }
  89.  
  90. public static void printAnagrams()
  91. {
  92. for (int i = 0; i < size; i++)
  93. {
  94. //System.out.print(charArray[i]);
  95. if(charArray[i] == words[i])
  96. {
  97. System.out.print(charArray[i]);
  98. }
  99.  
  100. }
  101. System.out.println();
  102. }
  103. }
Add Comment
Please, Sign In to add comment