Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. package hashtable;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Hashtable;
  6. import java.util.Scanner;
  7.  
  8. import javafx.application.Application;
  9. import javafx.event.Event;
  10. import javafx.event.EventHandler;
  11. import javafx.scene.Scene;
  12. import javafx.scene.control.Button;
  13. import javafx.scene.input.MouseEvent;
  14. import javafx.scene.layout.BorderPane;
  15. import javafx.scene.layout.HBox;
  16. import javafx.stage.FileChooser;
  17. import javafx.stage.Stage;
  18.  
  19. public class HashTableTest extends Application
  20. {
  21.  
  22. @Override
  23. public void start(Stage primaryStage)
  24. {
  25.  
  26.  
  27. Button importButton = new Button(" Import File ");
  28.  
  29.  
  30. /** button to solve the maze */
  31. importButton.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<Event>()
  32. {
  33.  
  34. @Override
  35. public void handle(Event event)
  36. {
  37. fileReader(primaryStage);
  38. }
  39. });
  40.  
  41.  
  42. HBox footer = new HBox();
  43. footer.setId("headingBox");
  44. footer.getChildren().addAll(importButton);
  45. BorderPane bp = new BorderPane();
  46. bp.setBottom(footer);
  47. //bp.setCenter();
  48. Scene scene = new Scene(bp);
  49. scene.getStylesheets().add("stack_maze/CssFile.css");
  50.  
  51. primaryStage.setScene(scene); // Place the scene in the stage
  52. primaryStage.show(); // Display the stage
  53. }
  54.  
  55. /** Main method to launch the javafx
  56. * @param args main method argument
  57. */
  58. public static void main(String[] args)
  59. {
  60. launch(args);
  61. //E names;
  62. String key;
  63.  
  64. // Creating a Hashtable
  65. Hashtable<String, String> hashtable =
  66. new Hashtable<String, String>();
  67.  
  68. // Adding Key and Value pairs to Hashtable
  69. hashtable.put("Key1","Chaitanya");
  70. hashtable.put("Key2","Ajeet");
  71. hashtable.put("Key3","Peter");
  72. hashtable.put("Key4","Ricky");
  73. hashtable.put("Key5","Mona");
  74.  
  75. //names = hashtable.keys();
  76. //while(names.hasMoreElements()) {
  77. // key = (String) names.nextElement();
  78. // System.out.println("Key: " +key+ " & Value: " +
  79. // hashtable.get(key));
  80. }
  81. public void fileReader(Stage primaryStage)
  82. {
  83. FileChooser fileChooser = new FileChooser();
  84.  
  85. // Setting it so you can only import text files
  86. FileChooser.ExtensionFilter extFilter =
  87. new FileChooser.ExtensionFilter("TEXT files (*.txt)", "*.txt");
  88. fileChooser.getExtensionFilters().add(extFilter);
  89.  
  90. // Show buttons to start program
  91. File file = fileChooser.showOpenDialog(primaryStage);
  92. if (file != null)
  93. {
  94. try
  95. {
  96. Scanner in = new Scanner(file);
  97.  
  98. SimpleList sl = new SimpleList();
  99. String line = in.nextLine();
  100. String[] words = line.split(" ");
  101. for(int i = 0; i < words.length; i++)
  102. {
  103. String wordCheck = words[i].toLowerCase();
  104. int wordIndex = sl.find(wordCheck);
  105. if(wordIndex > -1)
  106. {
  107. sl.getEntry(wordIndex).incrementCount();
  108. }
  109. else
  110. {
  111. Entry newEntry = new Entry(wordCheck);
  112. sl.add(newEntry);
  113. }
  114. sl.toString();
  115. }
  116.  
  117. }
  118. catch(IOException e)
  119. {
  120. System.err.println(e);
  121. System.exit(1);
  122. }
  123. }
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement