mardi 28 avril 2015

Issues with if statement

I am trying to find a keyword in a text file and return the sentence if found. If the keyword is not found I want to call a Writer method. But for some reason the Writer method always runs.

What needs to change to ensure the writer method is only called if the keyword is not found?

Here is my code

    private static HashMap<String, String[]> populateSynonymMap() {
   responses.put("professor", new String[]{"instructor", "teacher", "mentor"});
    responses.put("book", new String[]{"script", "text", "portfolio"});
    responses.put("office", new String[]{"room", "post", "place"});
    responses.put("day", new String[]{"time",  "date"});
    responses.put("asssignment", new String[]{"homework", "current assignment "});
    responses.put("major", new String[]{"discipline", "focus"," study"});


    return responses;
}

Here is my main method and the Writer Method

public static void main(String args[]) throws ParseException, IOException {
    /* Initialization */
    HashMap<String, String[]> synonymMap = new HashMap<String, String[]>();
    synonymMap= populateSynonymMap(); //populate the map

    Scanner in = new Scanner(System.in);
    Scanner scanner = new Scanner(System.in);
    String input = null;

    System.out.println("Welcome To DataBase ");
    System.out.println("What would you like to know?");

    System.out.print("> ");
    input = scanner.nextLine().toLowerCase();           
     String[] inputs = input.split(" ");

    boolean found = false;
    for(String ing : inputs){ //iterate over each word of the sentence.
        for (Map.Entry<String, String[]> entry : synonymMap.entrySet()) {
            String key = entry.getKey();
            String[] value = entry.getValue();
            if (key.equals(ing) || Arrays.asList(value).contains(ing)) {

                    found = true;
                    parseFile(entry.getKey());

                    System.out.println(" Would you like to update this information ? "); 
                String yellow = in.nextLine(); 
                if (yellow.equals("yes")) {
                    removedata(entry.getKey());
                }
                if (yellow.equals("no")) {
                    System.out.println("Have a good day"); 
                    break;
                }


                    break;
                    //System.out.println("Have a good day!");
                   // break;
            }
        }
        if(found){
             Writer();
        }   
    }
}
}


 public static void Writer() {
    boolean endofloop = false;
    Scanner Keyboard = new Scanner(System.in);
    Scanner input = new Scanner(System.in);

    File file = new File("data.txt");
    try (BufferedWriter wr = new BufferedWriter(new FileWriter(
            file.getAbsoluteFile(), true))) { // Creates a writer object
                                                // called wr
                                                // file.getabsolutefile
                                                // takes the filename and
                                                // keeps on storing the old
        System.out.println("I do not know, Perhaps you wanna help me learn?"); // data
        while ((Keyboard.hasNext())) {

            String lines = Keyboard.nextLine();
            System.out.print(" is this correct ? ");
            String go = input.nextLine();

            if (go.equals("no")) {
                System.out.println("enter what you want to teach me");
                lines = Keyboard.nextLine();
                System.out.print(" is this correct ? ");
                go = input.nextLine();
            }

            if (go.equals("yes")) {
                wr.write(lines);
                wr.write("\n");

                wr.newLine();

                wr.close();
            }

            System.out.println("Thankk you");
            break;
        }
    } catch (IOException e) {
        System.out.println(" cannot write to file " + file.toString());
    }
}

Aucun commentaire:

Enregistrer un commentaire