fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. package Exercise6;
  9.  
  10. import java.io.BufferedReader;
  11. import java.io.FileReader;
  12. import java.util.ArrayList;
  13. import java.util.Scanner;
  14. import java.io.*;
  15. import java.awt.*;
  16.  
  17.  
  18. public class LadderGame
  19. {
  20. public static void ladder(String startingword, String endingword, ArrayList<String> dictionary, ArrayList<String> history, int count)
  21. {
  22. String tempword = dictionary.get(count);
  23. count++;
  24. int count2 = 0;
  25.  
  26. for(int i = 0; i < tempword.length(); i++)
  27. if(startingword.charAt(i) != tempword.charAt(i))
  28. count2++;
  29.  
  30. if(count2 == 1)
  31. {
  32. startingword = tempword;
  33. history.add(startingword);
  34. }
  35. // else
  36. // if(tempword.equals(endingword))
  37. // round++;
  38.  
  39.  
  40. if(startingword.equals(endingword))
  41. System.out.println(endingword);
  42. else
  43. {
  44. if(count < dictionary.size())
  45. ladder(startingword, endingword, dictionary, history, count);
  46. // else if(count == dictionary.size() && round > 0)
  47. // {
  48. // ladder(startingword, endingword, dictionary, history, 0, round - 1);
  49. // }
  50. else
  51. System.out.println("No ladder found.");
  52. }
  53.  
  54.  
  55. }
  56.  
  57. public static void main(String[] args)
  58. {
  59. Scanner sc = new Scanner(System.in);
  60.  
  61. ArrayList<String> dictionary = new ArrayList<String>();
  62. ArrayList<String> history = new ArrayList<String>();
  63.  
  64.  
  65. System.out.println("Enter starting word.");
  66. String startingword = sc.nextLine().toUpperCase();
  67. System.out.println("Enter ending word.");
  68. String endingword = sc.nextLine().toUpperCase();
  69.  
  70. try
  71. {
  72. fr = new FileReader("/Users/Huangyonghuan/Downloads/words.txt");
  73. br = new BufferedReader(fr);
  74. int lines = countLines();
  75.  
  76.  
  77. for(int i = 0; i < lines; i++)
  78. {
  79. String readword = br.readLine().toUpperCase();
  80. if(readword.length() == endingword.length())
  81. dictionary.add(readword);
  82. }
  83. }
  84. catch (FileNotFoundException fnex)
  85. {
  86. System.err.println("Exception!");
  87. }
  88. catch (IOException e)
  89. {
  90. e.printStackTrace();
  91. }
  92.  
  93. ladder(startingword, endingword, dictionary, history, 0);
  94. }
  95.  
  96. public static int countLines()
  97. {
  98. int lines = 0;
  99. try
  100. {
  101. fr = new FileReader("/Users/Huangyonghuan/Downloads/words.txt");
  102. br = new BufferedReader(fr);
  103. while (br.readLine() != null) lines++;
  104. }
  105. catch (FileNotFoundException fnex)
  106. {
  107. System.err.println("Exception!");
  108. }
  109. catch (IOException e)
  110. {
  111. e.printStackTrace();
  112. }
  113. return lines;
  114. }
  115.  
  116. /* if(endingword.equals(readword))
  117.   for(int i = 0; i < endingword.length(); i++)
  118.   if(startingword.charAt(i) != readword.charAt(i))
  119.   tempword.charAt(i) = readword.charAt(i);*/
  120. /* fr = new FileReader("/Users/Huangyonghuan/Downloads/words.txt");
  121.   br = new BufferedReader(fr);
  122.   String readword = br.readLine().toUpperCase();
  123.  
  124.   if(endingword.length() == readword.length())
  125.   {
  126.   for(int i = 0; i < endingword.length(); i++)
  127.   if(endingword.charAt(i) != readword.charAt(i))
  128.   {
  129.   count++;
  130.   location = i;
  131.   }
  132.   if(count == 0)
  133.   System.out.println(endingword);
  134.   else if(count == 1)
  135.   {
  136.   for(int i = 0; i < location; i++)
  137.   tempword += endingword.charAt(i);
  138.   tempword += readword.charAt(location);
  139.   for(int i = location + 1; i < endingword.length(); i++)
  140.   tempword += endingword.charAt(i);
  141.   System.out.println(tempword);
  142.   ladder(tempword, endingword);
  143.   }
  144.  
  145.   }
  146.   else
  147.   return ladder(startingword, endingword);*/
  148. }
  149. /* if(tempword.equals(endingword))
  150.   {
  151.   if(count2 == 1)
  152.   System.out.println(endingword);
  153.   else if(count2 > 1)
  154.   round++;
  155.   }
  156.   else
  157.   if(count2 == 1 && !(history.contains(tempword)))
  158.   {
  159.   startingword = tempword;
  160.   history.add(startingword);
  161.   }*/
  162.  
  163. /* do
  164.   {
  165.   String readword = br.readLine().toUpperCase();
  166.   if(readword.length() == endingword.length())
  167.   dictionary.add(readword);
  168.   if(readword.equals(""))
  169.   t++;
  170.   }while(t == 0);*/
  171.  
  172. /* String tempword = dictionary.get(count);
  173.   count++;
  174.   int count2 = 0;
  175.  
  176.   for(int i = 0; i < tempword.length(); i++)
  177.   if(startingword.charAt(i) != tempword.charAt(i))
  178.   count2++;
  179.  
  180.   if(count2 == 1)
  181.   {
  182.   startingword = tempword;
  183.   history.add(startingword);
  184.   }
  185. // else
  186. // if(tempword.equals(endingword))
  187. // round++;
  188.  
  189.  
  190.   if(startingword.equals(endingword))
  191.   System.out.println(endingword);
  192.   else
  193.   {
  194.   if(count < dictionary.size())
  195.   ladder(startingword, endingword, dictionary, history, count, round);
  196. // else if(count == dictionary.size() && round > 0)
  197. // {
  198. // ladder(startingword, endingword, dictionary, history, 0, round - 1);
  199. // }
  200.   else
  201.   System.out.println("No ladder found.");
  202.   }*/
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:8: error: class, interface, or enum expected
package Exercise6;
^
1 error
stdout
Standard output is empty