Hello,
I'm having issues with searching for a word in a file. The word is a user input from console, then it should look in a file for it. And print out the position. I'm using indexOf() for this.
Can you please tell me whats wrong with my code? My problem is that when I insert the word and then try to look for it in the file,it doesnt work.
this is the code for The input
if(input.startsWith("modify")) { // Call the modify method and pass a string array as argument which does not // include "modify" and has split the remaining arguments by whitespace. list(); System.out.println("Please insert the id of the file which you wish to modify: "); Scanner scanner = new Scanner(System.in); choose = scanner.nextLine(); System.out.println("In order to modify an event you can change one field at the time" + "\n" + " write the name of the field and then write the new Value"); Changefield = scanner.nextLine(); String newValue = scanner.nextLine(); modify(new String[]{choose,Changefield,newValue} ); //modify(input.substring(7).split("\\s+")); } // Fall through case // System.out.println("Illegal command: " + input); }while(!input.equals("quit")); // Cleanup. System.out.println("EventManager is terminating..."); in.close(); }
And here is the code for checking the input if it's in the file:
try { BufferedReader in = null; try { in = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } in.readLine(); System.out.println("Searching for :" + Changefield + " in file: " + file.getName()); String line; while((line = in.readLine()) != null) { int index = line.indexOf(Changefield); System.out.println(" Position of " + Changefield + "is at the position: " + index); } Path from = Paths.get(String.valueOf(file)); Path to = Paths.get("D:\\Uni.lu\\events\\" + id + ".events-Temp.txt"); CopyOption[] option = new CopyOption[]{ StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES }; for (Path path : Files.copy(from, to, option)) { } } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } }
Thank you for your help,there must be sth wrong with the loop, it doesnt seems to be going into th while to check if the input exists.