java - Check if scanner input equals list that is converted to arraylist -
java - Check if scanner input equals list that is converted to arraylist -
i have file bunch of strings in it. have converted whatever in file list. want create scanner scans user-input , returns true or false whether scanner input contains whatever in list (file).
i have logic in head don't know syntax.
i have come far. need help. in advance.
public static void main (string [] args) throws exception { scanner scan = new scanner(new file("/users/greg/workspace/erlang/fred.txt")); arraylist<string> list = new arraylist<string>(); while (scan.hasnext()){ list.add(scan.next()); scanner scan2 = new scanner (system.in); system.out.println("enter"); string input = scan2.nextline(); // if input equals list / file - > sysout true . else > false }
by help of amazing people on stackoverflow completed assignment. finished code , working perfectly:¨
public static void main(string[] args) throws exception { scanner scan = new scanner(new file("/users/greg/workspace/erlang/fred.txt")); arraylist<string> list = new arraylist<string>(); while (scan.hasnext()) { list.add(scan.next()); } { string input = null; scanner scan2 = new scanner(system.in); input = scan2.nextline(); if (list.contains(input)) { system.out.println("yes, list/file contains: " + input); } else { system.out.println("no, list/file not contain: " + input); } } while (true); } { } { }
i created bufferedreader
reads in input system.in
, checked if list contains line read in, , wrote out accordingly.
public static void main (string [] args) throws exception { scanner scan = new scanner(new file("/users/greg/workspace/erlang/fred.txt")); arraylist<string> list = new arraylist<string>(); while (scan.hasnext()){ list.add(scan.next()); bufferedreader bufferedreader = null; string line = null; seek { bufferedreader = new bufferedreader(new inputstreamreader(system.in)); { line = bufferedreader.readline(); system.out.println("" + list.contains(line)); } while(!"exit".equals(line)); } catch(ioexception e) { e.printstacktrace(); } { if(bufferedreader != null) seek { bufferedreader.close(); } catch(ioexception e) {} } }
java list
Comments
Post a Comment