Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...