Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. /* SearchCmd.java
  2. Written by Rune Hansen
  3. Updated by Alexandre Buisse <abui@itu.dk>
  4. */
  5.  
  6. import java.io.*;
  7.  
  8. class HTMLlist
  9. {
  10. String str;
  11. HTMLlist next;
  12.  
  13. HTMLlist (String s, HTMLlist n) //Constructor
  14. {
  15. str = s;
  16. next = n;
  17. }
  18. }
  19.  
  20. class Searcher
  21. {
  22.  
  23. public static boolean exists (HTMLlist l, String word) //Static method. So it starts with the first node and cheks if the String word (the word we're searching for is in the list
  24. {
  25. //The l-Node is the CurrentNode
  26. while (l != null)
  27. {
  28. if (l.str.equals (word)) //finds the word
  29. {
  30. return true;
  31. }
  32. l = l.next; //if it didn't find the word it goes to the next item
  33. }
  34. return false; //if the word was not in the list
  35. }
  36.  
  37. public static HTMLlist readHtmlList (String filename) throws IOException
  38. { //This method builds the list from the beginning because the words have to be in the same order as they are in the file
  39. String name;
  40. HTMLlist start, current, tmp; //start=firstNode. Three fields?
  41.  
  42. // Open the file given as argument
  43. BufferedReader infile = new BufferedReader(new FileReader(filename));
  44.  
  45. name = infile.readLine(); //So first we read the first node(word in the list) from the file
  46. start = new HTMLlist (name, null); //creates the firstNode by using it's name for the item and puts its nextreference to null
  47. current = start; //sets the item to be the first
  48. name = infile.readLine(); // Read the next line of the (txt)file
  49. while (name != null) { // Exit if it is null. goes to close
  50. tmp = new HTMLlist(name,null); //creates a new node with the word from the file in it and puts it on the tmpfield
  51. current.next = tmp; //the old next.field points to the new item
  52. current = tmp; // Update the linked list???
  53. name = infile.readLine(); // Read the next line
  54. }
  55. infile.close(); // Close the file
  56.  
  57. return start;
  58. }
  59. }
  60.  
  61. public class SearchCmd
  62. {
  63.  
  64. public static void main (String[] args) throws IOException
  65. {
  66. String name;
  67.  
  68. // Check that a filename has been given as argument
  69. if (args.length != 1)
  70. {
  71. System.out.println("Usage: java SearchCmd <datafile>");
  72. System.exit(1);
  73. }
  74.  
  75. // Read the file and create the linked list
  76. HTMLlist l = Searcher.readHtmlList (args[0]);
  77.  
  78. // Ask for a word to search
  79. BufferedReader inuser =
  80. new BufferedReader (new InputStreamReader (System.in));
  81.  
  82. System.out.println ("Hit return to exit.");
  83. boolean quit = false;
  84. while (!quit) {
  85. System.out.print ("Search for: ");
  86. name = inuser.readLine(); // Read a line from the terminal
  87. if (name == null || name.length() == 0)
  88. {
  89. quit = true;
  90. }
  91. else if (Searcher.exists (l, name)) {
  92. System.out.println ("The word \""+name+"\" has been found.");
  93. }
  94. else
  95. {
  96. System.out.println ("The word \""+name+"\" has NOT been found.");
  97. }
  98. }
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement