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?
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: