java - Issues with if statement -
i trying find keyword in text file , return sentence if found. if keyword not found want call writer method. reason writer method runs.
what needs change ensure writer method called if keyword not found?
here 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 main method , writer method
public static void main(string args[]) throws parseexception, ioexception { /* initialization */ hashmap<string, string[]> synonymmap = new hashmap<string, string[]>(); synonymmap= populatesynonymmap(); //populate map scanner in = new scanner(system.in); scanner scanner = new scanner(system.in); string input = null; system.out.println("welcome database "); system.out.println("what know?"); system.out.print("> "); input = scanner.nextline().tolowercase(); string[] inputs = input.split(" "); boolean found = false; for(string ing : inputs){ //iterate on each word of sentence. (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(" update information ? "); string yellow = in.nextline(); if (yellow.equals("yes")) { removedata(entry.getkey()); } if (yellow.equals("no")) { system.out.println("have day"); break; } break; //system.out.println("have 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 writer object // called wr // file.getabsolutefile // takes filename , // keeps on storing old system.out.println("i not know, perhaps wanna me learn?"); // data while ((keyboard.hasnext())) { string lines = keyboard.nextline(); system.out.print(" correct ? "); string go = input.nextline(); if (go.equals("no")) { system.out.println("enter want teach me"); lines = keyboard.nextline(); system.out.print(" 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 file " + file.tostring()); } }
if want call writer if not found, must use:
if (!found) writer();
your break terminating inner loop, might want add
if (found) break;
to terminate outer loop (you did not if required or not).
Comments
Post a Comment