表达式条件的动态索引,例如 While、If 等
dynamic index for an expression condition such as While, If etc
谈到 for-loop
,我想要 for-loop
中的动态条件。为了更清楚,这是我为您提供的代码:
FileOutputStream Fcategorize = new FileOutputStream(write, true);
FileReader inputFile = new FileReader(wrote);
BufferedReader bufferReader = new BufferedReader(inputFile);
String line;
for (int i = 0; (line = bufferReader.readLine()) != null; i++) {
if ("this2".equals(line)) {
while (!"this3".equals(line = bufferReader.readLine())) {
Fcategorize.write(line.toLowerCase().getBytes());
Fcategorize.write(
System.getProperty("line.separator").getBytes());
}
}
}
然而,我想要的是 this2
和 this3
都在每次迭代中更改它们的整数 属性,关于 i
的值。怎么可能?
编辑:
示例文件内容:
this2
primarygames
storybooks
bookshelf.htm
this3
classzone
books
research_guide
page_build.cfm
this4
grandmasandy
books-info.html
this5
soiwasthinkingaboutadoption
free_book.html
this6
slcpd
c0ntent
uploads
activity-book.pdf
this7
what I want is that both this2 and this3 change their integer property in each iteration, regarding to the value of i.
所以你需要在循环中根据i
动态创建字符串this2
和this3
。
因此,请尝试以下操作:
String thisWithIterator;
//... some code
for (int i=0;(line = bufferReader.readLine()) != null;i++) {
thisWithIterator = "this"+(i+2);
if(thisWithIterator.equals(line)) {
//... some code
thisWithIterator = "this"+(i+3);
while(!thisWithIterator.equals(line=bufferReader.readLine())) {
//... rest of the codes goes here
一个干净的解决方案可能使用一种方法来计算要匹配的字符串,基于当前行或组号。
static String header( int i ){
return "this" + (i+2); // "this2" for i == 0
}
static String body( int i ){
return "this" + (i+3); // "this3" for i == 0
}
读取循环变为
String line = bufferReader.readLine();
for(int i = 0; line != null; i++){
if( header(i).equals(line)){
while( (line = bufferReader.readLine()) != null &&
! body(i).equals( line ) ){
Fcategorize.write(...);
Fcategorize.write(...);
}
}
}
请注意,我还在内部循环中添加了 EOF 检查。
如果您确实希望字符串组成符合要求,请使用
if( ("this" + (i+2)).equals( line ) ){...}
或者,如果保护得当,行永远不会为空
if( line.equals( "this" + (i+2) ) ){...}
算术加必须优先考虑;否则它将连接到 "this02"、"this12" 等
Edit 该算法本身就很危险,因为如果第一行不包含 "this2",它可能会进入无限循环。最好在一个循环中读取并测试行,看看你在哪里。
String line;
int expect = 0;
int body = 0;
while( (line = bufferReader.readLine()) != null ){
if( line.equals( header(expect) ) ){
body = expect;
expect++;
} else if( body > 0 ) {
Fcategorize.write(...); // process body
}
}
谈到 for-loop
,我想要 for-loop
中的动态条件。为了更清楚,这是我为您提供的代码:
FileOutputStream Fcategorize = new FileOutputStream(write, true);
FileReader inputFile = new FileReader(wrote);
BufferedReader bufferReader = new BufferedReader(inputFile);
String line;
for (int i = 0; (line = bufferReader.readLine()) != null; i++) {
if ("this2".equals(line)) {
while (!"this3".equals(line = bufferReader.readLine())) {
Fcategorize.write(line.toLowerCase().getBytes());
Fcategorize.write(
System.getProperty("line.separator").getBytes());
}
}
}
然而,我想要的是 this2
和 this3
都在每次迭代中更改它们的整数 属性,关于 i
的值。怎么可能?
编辑:
示例文件内容:
this2
primarygames
storybooks
bookshelf.htm
this3
classzone
books
research_guide
page_build.cfm
this4
grandmasandy
books-info.html
this5
soiwasthinkingaboutadoption
free_book.html
this6
slcpd
c0ntent
uploads
activity-book.pdf
this7
what I want is that both this2 and this3 change their integer property in each iteration, regarding to the value of i.
所以你需要在循环中根据i
动态创建字符串this2
和this3
。
因此,请尝试以下操作:
String thisWithIterator;
//... some code
for (int i=0;(line = bufferReader.readLine()) != null;i++) {
thisWithIterator = "this"+(i+2);
if(thisWithIterator.equals(line)) {
//... some code
thisWithIterator = "this"+(i+3);
while(!thisWithIterator.equals(line=bufferReader.readLine())) {
//... rest of the codes goes here
一个干净的解决方案可能使用一种方法来计算要匹配的字符串,基于当前行或组号。
static String header( int i ){ return "this" + (i+2); // "this2" for i == 0 } static String body( int i ){ return "this" + (i+3); // "this3" for i == 0 }
读取循环变为
String line = bufferReader.readLine(); for(int i = 0; line != null; i++){ if( header(i).equals(line)){ while( (line = bufferReader.readLine()) != null && ! body(i).equals( line ) ){ Fcategorize.write(...); Fcategorize.write(...); } } }
请注意,我还在内部循环中添加了 EOF 检查。
如果您确实希望字符串组成符合要求,请使用
if( ("this" + (i+2)).equals( line ) ){...}
或者,如果保护得当,行永远不会为空
if( line.equals( "this" + (i+2) ) ){...}
算术加必须优先考虑;否则它将连接到 "this02"、"this12" 等
Edit 该算法本身就很危险,因为如果第一行不包含 "this2",它可能会进入无限循环。最好在一个循环中读取并测试行,看看你在哪里。
String line; int expect = 0; int body = 0; while( (line = bufferReader.readLine()) != null ){ if( line.equals( header(expect) ) ){ body = expect; expect++; } else if( body > 0 ) { Fcategorize.write(...); // process body } }