Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:30:55+00:00 2026-06-17T04:30:55+00:00

My text file contains: Hello This is a Test Press Enter to Continue I

  • 0

My text file contains:

Hello This is a Test
Press Enter to Continue

I have an array of:

int StartIndex [] = {1,4,8}
int EndIndex [] = {3,7,11}

String[] VALUES = new String[] {"Sys","Jav","Tes"};

I want to replace index{1,3} with ‘Sys’, index{4,7} with ‘Jav’ and so on in the file.

My idea is to read the whole file as a String and then pass on the indexes to replace with the VALUES Strings.

How can I do this ?

CODE:

 String[] VALUES = new String[] {"Sys"}; //Correct Solutions

 int [] StartIndex ={4};
 int [] EndIndex ={6};
 while ((line = br.readLine()) != null)   {
                  // Print the content on the console
                  System.out.println (line);
                  StringBuffer buf = new StringBuffer(line);
                  buf.replace(StartIndex[0], EndIndex[0], VALUES[0]);
                  done =buf.toString();
                  System.out.println(done);

Expected Ouput should be like this:

SyslJavhTes is a Test
Press Enter to Continue

I searched a bit and got this:

String myName = "domanokz";
char[] myNameChars = myName.toCharArray();
myNameChars[4] = 'x';
myName = String.valueOf(myNameChars);

If we convert the file to string and apply this function ,will this work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:30:56+00:00Added an answer on June 17, 2026 at 4:30 am

    Problem Solved! Code works perfectly because I tested it. Like before no comments have been added so you will understand & learn. (Please vote/accept answer if it works for others to identify the correct answer).

    CODE:

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    /**
     *
     * @author jtech
     */
    public class ReplaceWithIndexes
    {
        public static void main(String[] args) throws IOException
        {
            BufferedReader br = null;
            boolean endMatched = false;
            int startIndex[] = {0,4,8};
            int endIndex[] = {3,7,10};
            int c = 0, c1 = 0, c2 = 0, largestVal_start = 0, largestVal_end = 0, lineCount = 0;
            String line = null, newString = "";
            String[] VALUES = new String[] {"Sys","Jav","Tes"};  
    
            br = new BufferedReader(new FileReader("C:\\Users\\jtech\\Documents\\NetBeansProjects\\HelpOthers\\src\\textFiles\\AnotherFile.txt"));
    
            for (int i = 0; i < startIndex.length; i++)
            {
                if (startIndex[i] > largestVal_start)
                {
                    largestVal_start = startIndex[i];
                }
            }
    
            for (int i = 0; i < endIndex.length; i++)
            {
                if (endIndex[i] > largestVal_end)
                {
                    largestVal_end = endIndex[i];
                }
            }       
    
    
                while ((line = br.readLine()) != null)   
                {
    
                    StringBuilder buf = new StringBuilder(line);
                              // Print the content on the console
                    System.out.println(line);
                    lineCount++;
    
                        while (c <= largestVal_start)
                        {                       
                           while (c1 <= largestVal_end)
                           {                           
                               if (startIndex[0] == c && endIndex[0] == c1)
                               {
                                 buf.replace(startIndex[0], endIndex[0], VALUES[c2]);
                                 newString = buf.toString();
                                 endMatched = true;
                               }
                               else if (startIndex[1] == c && endIndex[1] == c1)
                               {
                                 buf.replace(startIndex[1], endIndex[1], VALUES[c2]);
                                 newString = buf.toString();
                                 endMatched = true;
                               }
                               else if (startIndex[2] == c && endIndex[2] == c1)
                               {
                                 buf.replace(startIndex[2], endIndex[2], VALUES[c2]);
                                 newString = buf.toString();
                                 endMatched = true;
                               }
    
                             c1++;
                           }
    
                          for (int i = 0; i < startIndex.length; i++)
                          {
                            if (c == startIndex[i])
                            {
                                c2++;
                            }
    
                          }
    
                          if (endMatched == true || ((c1 <= largestVal_end) == false) )
                          {
                              c1 = 0;
                              endMatched = false;
                          }
    
                          c++;
    
                        }
    
                    if (lineCount <= 1)
                    {
                      System.out.println("Updated line: " + newString);
                    }
    
                }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file which contains something like this: Hello, my name is
I have a text file which contains only lines with a message like: Hello
My raw input file text file contains a string: Caf&eacute (Should be Café) The
I have a text file that contains cached data in JSON format. I'm trying
I have a text file that contains some strings separated by ,. Strings are
I have a text file that contains a list of filenames, minus the extension,
I have a text file which contains data seperated by '|'. I need to
I have Text file that contains data separated with a comma , . How
I have a text file that contains unix style line endings: a 0x0A at
What I have is a giant text file that contains a bunch of strings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.