Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. package comp2402a1;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.PrintWriter;
  9. import java.util.*;
  10.  
  11. public class Part6 {
  12.  
  13. /**
  14. * Your code goes here - see Part0 for an example
  15. * @param r the reader to read from
  16. * @param w the writer to write to
  17. * @throws IOException
  18. */
  19. public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
  20. Set<String> s = new HashSet<>();
  21. String line;
  22. for (line = r.readLine(); line != null; line = r.readLine()) {
  23. if(!s.contains(line)){
  24. w.println(line);
  25. }
  26. String illegalString = "";
  27. for (int i=line.length() -1;i>=0;i--){
  28. String currentCharacter = Character.toString(line.charAt(i));
  29. illegalString = currentCharacter + illegalString;
  30. s.add(illegalString);
  31.  
  32. }
  33.  
  34. }
  35. }
  36. /*
  37. while((line=r.readLine()) != null){
  38. b = false; //reset boolean for each line
  39. for(String text: s){
  40. if(text.endsWith(line)) {
  41. b = true;
  42. break;
  43. }
  44. }
  45. if (!b) { //if not line from s ends with the new added line, then print it to w
  46. w.println(line);
  47. }
  48. s.add(line); //add line to s
  49.  
  50. }//end while
  51. */
  52.  
  53.  
  54. /**
  55. * The driver. Open a BufferedReader and a PrintWriter, either from System.in
  56. * and System.out or from filenames specified on the command line, then call doIt.
  57. * @param args
  58. */
  59. public static void main(String[] args) {
  60. try {
  61. BufferedReader r;
  62. PrintWriter w;
  63. if (args.length == 0) {
  64. r = new BufferedReader(new InputStreamReader(System.in));
  65. w = new PrintWriter(System.out);
  66. } else if (args.length == 1) {
  67. r = new BufferedReader(new FileReader(args[0]));
  68. w = new PrintWriter(System.out);
  69. } else {
  70. r = new BufferedReader(new FileReader(args[0]));
  71. w = new PrintWriter(new FileWriter(args[1]));
  72. }
  73. long start = System.nanoTime();
  74. doIt(r, w);
  75. w.flush();
  76. long stop = System.nanoTime();
  77. System.out.println("Execution time: " + 10e-9 * (stop-start));
  78. } catch (IOException e) {
  79. System.err.println(e);
  80. System.exit(-1);
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement