Advertisement
SensaBG

ExamEd

Dec 1st, 2024 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class newTwo {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         Map<String, StringBuilder> book = new HashMap<>();
  11.         String[] notes = scanner.nextLine().split(" \\| ");
  12.  
  13.         for (String note : notes) {
  14.             String[] data = note.split(": ");
  15.             String word = data[0];
  16.             String def = data[1];
  17.  
  18.             book.putIfAbsent(word, new StringBuilder());
  19.  
  20. // NOTE!
  21. // System.lineSeparator() runs on all systems effectively, whereas the "\n" might interfere with other systems like Linux
  22.  
  23. //removed the ".append(" -")" as we can do it on the printing instead
  24. // replaced the System.lineSeparator() with "\n",
  25.             book.get(word).append(def).append("\n"); // now we just append the "def" String variable in the StringBuilder
  26. // adding "\n" -> ensuring new info related to a Title ( word ) is on a new line
  27. //   book.get(word).append(" -").append(def).append(System.lineSeparator());
  28.         }
  29.  
  30.         notes = scanner.nextLine().split(" \\| ");
  31.         String command = scanner.nextLine();
  32.  
  33. // you can still use the below boolean variable for the if on line 49 - if you think it's no longer confusing
  34. // boolean isExamTime = "Test".equals(command);
  35. //  if("Test".equals(command)) ->  if(isExamTime){
  36.  
  37.  
  38.         for (String currentTopic : notes) {
  39.  
  40. // renamed to topicFromBook as its more accurate
  41. // replaced the System.lineSeparator() with "\n",
  42.             String topicFromBook = "\n" + book.get(currentTopic);
  43. //String topicFromBook = System.lineSeparator() + book.get(currentTopic);
  44.  
  45. //split the ternary operator into an if else if statement
  46. //System.out.print(isExamTime ? currentTopic + ":" + topicFromBook : currentTopic + " ");
  47.  
  48.             if (book.containsKey(currentTopic)) {
  49. // first we have to check our book with past notes if it contains the particular topic(s) our teacher is asking us
  50.                 if("Test".equals(command)){
  51.                     System.out.println(currentTopic + (":") + (" -") +  topicFromBook);
  52.                 } else if ("Hand Over".equals(command)){
  53.                     System.out.println(currentTopic + " ");
  54.                 }
  55. // print only the topics saved in your book in the order being asced depending on whether is an:
  56. //    1. exam  -> print  everything "Title(s)" ( word ) and Paragraph(s) ( def )
  57. //    2. brief check  -> print only the "Title(s)" ( word )
  58.             }
  59.         }
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement