Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to change a string at indexOf

  1. #1
    Join Date
    Apr 2012
    My Mood
    Thanked 0 Times in 0 Posts

    Default How to change a string at indexOf


    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:
    In 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
    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
    Thank you very for your help

  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    North Wales
    My Mood
    Thanked 115 Times in 106 Posts
    Blog Entries

    Default Re: How to change a string at indexOf

    If you want to replace only the first or all words in your String which match a specific character sequence, you could just use String.replaceFirst() or String.replaceAll()

    However, if you need to replace between an two indicies, then this method should do it:

    	public String replaceBetweenRange(String original, String replacement, int start, int end){
    		return original.replace(original.substring(start, end ), replacement);
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. What is the easiest way to change a single character in a string?
    By Jumbosize in forum Java Theory & Questions
    Replies: 2
    Last Post: April 26th, 2012, 04:16 PM
  2. Can I change attributedstring into string?
    By star12345645 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2012, 08:02 AM
  3. Replies: 7
    Last Post: December 11th, 2011, 11:58 PM
  4. How to change a String value into a number and then back into a String.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 18th, 2011, 01:43 PM
  5. contains vs. indexOf
    By mgrootsch in forum Java Theory & Questions
    Replies: 5
    Last Post: September 17th, 2009, 08:09 AM