Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
907 views
in Technique[技术] by (71.8m points)

java - Grouping strings from a file

there is a text file, some code is stored there, how can I to group for example the whole for loops, or if, while, and then write them to the collection

For example there is a file that I read and wrote into an array:

0)Class A{
1)if(a>0){
2)a=a+1:
3)System.out.print(a);
4)}
5)if(a<0){
6)for(i=0; i<s.length; i++){
7)words.add(s[i]);
8)}
9)for(i=0; i<s.length; i++){
10)words.add(s[i]);
11)}
12)}
13)for(j=0; j<sap.length; j++){
14)Rep.add(s[i]);
15)System.out.println(s[i]);
16)}
17)for(j=0; j<sap.length; j++){
18)Rep.add(s[i]);
19)System.out.println(s[i]);
20)}
21)}

After regrouping, he should become this:

0)Class A{
1)if(a>0){
a=a+1:
System.out.print(a);
}
2)if(a<0){
3)for(i=0; i<s.length; i++){
words.add(s[i]);
}
4)for(i=0; i<s.length; i++){
words.add(s[i]);
}
}
5)for(j=0; j<sap.length; j++){
Rep.add(s[i]);
System.out.println(s[i]);
}
6)for(j=0; j<sap.length; j++){
Rep.add(s[i]);
System.out.println(s[i]);
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use a Stack to balance braces. See this video. Your situation is very similar, this should put you in the right direction.

Here is the actual implementation of Stack

Edit: Being downvoted? Please explain, this is the direction that should be taken unless someone else has a better idea.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...