Hello,
I would like to change a string at the position of indexof. But I cant figure out how to do it. Should I use substring? in order to do it? And if yes how? Because I'm getting errors
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. } 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); System.out.println("Print" + Changefield.substring(index)); // System.out.println("Old value " + Changefield + " new value : " + newValue); }
The Changefield and newValue are user input variables. Changefiled is a variable which exists in a file and newValue should replace the Changefield.
here are the errors:Thank you very for your helpIn order to modify an event you can change one field at the time
write the name of the field and then write the new Value
14:00
16:00
Your choice was: 5Searching for :14:00 in file: 5.events.txt
Position of 14:00is at the position: 11
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -6
at java.lang.String.substring(String.java:1875)
at com.DistributedS.EventManager.modify(EventManager. java:292)
at com.DistributedS.EventManager.Start(EventManager.j ava:168)
at com.DistributedS.Main.main(Main.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main (AppMain.java:120)
Process finished with exit code 1