Task: there is a string like

(zzzz (aaaa "bbbb1 bbbb2" (ccccc "dddd1 dddd2" (eeeee)) ffff (gggg hhhh) iiiii jjjj))

It is necessary to write a recursion program in Java, which translates such a list into an ArrayList with multi-level nesting, i.e. the bracket in line is a recess to the level, the element of the arraylist itself becomes an arraylist, etc., for example, in this sample line on the top level there are two elements: zzzz and the arraylist of aaaa bbbb are still an arrey sheet from cccc dddd and so on .. .
Quoted strings should be taken as a single block, i.e. from quotes to quotes!

Please help me with the algorithm, otherwise my version does not work correctly ( Gson not pay attention to Gson ):

 import com.google.gson.Gson; import java.util.ArrayList; public class Parser { String inputString; String commandLine; public Parser(String in,String com){ this.inputString=in; this.commandLine=com; } public String parse() { Gson json = new Gson(); ArrayList result=getDevicesList(); return json.toJson(result); } private ArrayList getDevicesList() { ArrayList result=getBracketBlock(0,this.inputString); return result; } private ArrayList getBracketBlock(int pos, String in) { ArrayList buffer = new ArrayList(); String token = ""; Boolean IS_STRING = false; String source = in.substring(pos); char [] chr = source.toCharArray(); pos=0; while( chr[pos]!=')') { if( chr[pos]=='(' ) { buffer.add(getBracketBlock(pos+1,source.substring(pos))); } if( chr[pos]==' ' && !token.equals("")) { if(!IS_STRING) { buffer.add(token);System.out.println(token+" "+pos); token=""; } } if(chr[pos]=='\"') IS_STRING=!IS_STRING; token+=chr[pos]; pos++; } buffer.add(token); return buffer; } } 
  • five
    I give advice so far ... sit down write the algorithm on paper, paint it with markers, understand - program - Gorets
  • Gorets, what do you think, why did I create a question here? .. I need help, and not felt-tip pens !!! - deivan_
  • 2
    @deivan - quick help is on freelance :) - Zowie
  • guys, you are helping me so well, earthly bow to you, what would you do without you - deivan_
  • Well, for example, I would help .. but I don’t still do it for you .. by the way, I just tried to write .. so far I quickly have nothing to do and I don’t like to use recursion here, better state machine. Or just this way ... String [] strings = "(zzzz (aaaa bbbb (ccccc ddddd (eeeee)) ffff (gggg hhhh) iiiii jjjj))". Split (""); for (String s: strings) { - Gorets am

2 answers 2

So, I answer myself. Here is the working code that solves my problem:

 import java.util.ArrayList; public class Parser { String inputString; String commandLine; int pos; public Parser(String in,String com){ this.inputString=in; this.commandLine=com; this.pos=0; } public String parse() { return getDevicesList().toString(); } private ArrayList getDevicesList() { ArrayList result=getBracketBlock(); return result; } private ArrayList getBracketBlock() { ArrayList buffer = new ArrayList(); String token = ""; Boolean IS_STRING = false; char [] chr = this.inputString.toCharArray(); while( pos<inputString.length()-1) { if(chr[pos]==' ' && !IS_STRING) { if(!token.equals("") && !token.equals(" ") && !token.equals(" ")) { buffer.add(token.trim()); token=""; pos++; continue; } } if(chr[pos]==')' && !IS_STRING) { if(!token.equals("") && !token.equals(" ")) buffer.add(token); break; } if(chr[pos]=='(') { pos++; buffer.add(getBracketBlock()); pos++; continue; } if(chr[pos]=='\"') IS_STRING=!IS_STRING; token+=chr[pos]; pos++; } return buffer; } } 

Not ideal, but quite working decision which illustrates the decision of tasks on parsing of the list data.
I hope this algorithm will help someone in the forum in the future.
Let the question hang open until the end of the day, for constructive comments, then close.
Thank you all for your attention.

  • String token = ""; ... if (chr [pos] == '' &&! IS_STRING) {if (! token.equals ("") &&! token.equals ("") &&! token.equals ("")) {buffer. add (token.trim ()); token = ""; hellish code, I read - if the position equals the space (hr [pos] == '') and does not equal not 1 non-gap, not 2 m, not 3 .. I suspect something is wrong here ... why the 2nd if? - Gorets pm
  • getDevicesList () is superfluous, why getBracketBlock () to wrap in getDevicesList ()? - Gorets 1:08 pm
  • nothing hellish, everything is orthodox :) look carefully: chr [] is an array with the source string, token is the current word, which is typed character-by-character and then added to the firelist. but somewhere I overslept, and sometimes the token is left empty or just one space - and this should not be in the set. therefore, I check that empty or white-space tokens do not go into the set (this is the second IF), however, spaces are allowed if the token is a string, i.e. consists of several words, for this there is a flag IZ_STRING (first if) - deivan_
  • Well, about double wrapping - this is not important, since the project for which this task is being solved has several sources of such lines, and for each source there will be a different class for parsing - deivan_
  • Well, that coped with the task, but I would recommend, if you have time, read the information about the state machines, they better cope with such tasks and do not make such confusion ... - Gorets
 String strings = "(zzzz (аааа bbbb (ccccc ddddd (eeeee)) ffff (gggg hhhh) iiiii jjjj))"; int i = 0; while (i < strings.length()) { i++; if (strings.charAt(i) == '(') { } if (strings.charAt(i) == ')') { } } 

try something like this ...

  • yep oil is oily. and where is recursion? - deivan_
  • so that's just the point .. where is the recursion? those. How can I apply it? I for example do not see the point .. or do you need to make it exactly by recursion? tell us how this recursion should work here ... - Gorets
  • in my code example there is a recursion, only it displays nonsense, I can not understand where I was wrong. As for the meaning of recursion, it’s obvious that the code that catches the list in brackets is typical, you need to write it and use it recursively for the whole line. - deivan_