【发布时间】:2013-01-11 12:04:07
【问题描述】:
我的文本文件包含:
Hello This is a Test
Press Enter to Continue
我有一个数组:
int StartIndex [] = {1,4,8}
int EndIndex [] = {3,7,11}
String[] VALUES = new String[] {"Sys","Jav","Tes"};
我想将文件中的 index{1,3} 替换为 'Sys',将 index{4,7} 替换为 'Jav' 等等。
我的想法是将整个文件作为字符串读取,然后传递索引以替换为 VALUES 字符串。
我该怎么做?
代码:
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);
预期的输出应该是这样的:
SyslJavhTes is a Test
Press Enter to Continue
我搜索了一下,得到了这个:
String myName = "domanokz";
char[] myNameChars = myName.toCharArray();
myNameChars[4] = 'x';
myName = String.valueOf(myNameChars);
如果我们将文件转换为字符串并应用这个函数,这会起作用吗?
【问题讨论】:
-
这是作业吗?请适当添加标签。关于您的问题,您面临的问题是什么?
-
我想用系统替换 index{1,3},用 Java 替换 index{4,7} 等等。如何在循环中做到这一点?
-
@sundar 自去年夏天以来,作业没有标签。
-
你甚至没有说你想替换什么。您可以发布您想要的输出以提供帮助吗?
-
检查已经发布的输出。 1 主要问题是第一次替换后索引被打乱了。