Hi everyone help please with the correction of the code, there is such a code:

class C { public static void main(String [] args) throws FileNotFoundException { String rows[] = new Scanner(new File("file.txt")).useDelimiter("\\Z").next().split("\n"); for (int i = 0; i < rows.length; i++) rows[i] = rows[i].trim(); System.out.println("------------------------------------------------------------------"); List<String> newLines = new ArrayList<>(); StringBuilder newLine = new StringBuilder(); for (String line : rows) { if (line.endsWith("{")) { if (newLine.length() > 0) { newLines.add(newLine.toString()); } newLine = new StringBuilder(line); } else { newLine.append("\n").append(line); } } newLines.add(newLine.toString()); } } 

the program should read from the file where there is some code in text form, and group separately If While for from, but this is what it displays on the screen

(1 option)

 0)'Class A{' 1)'if(a==0){ System.out.println(a); }' 2)'if(a==0){ System.out.println(a); } }' 

and I need the 2 element to be grouped like this, before its closing bracket

(Option 2)

  0)'Class A{ 1)'if(a==0){ System.out.println(a); } ' 2)'if(a==0){ System.out.println(a); }' }' 

I do not understand where the error is, and why the result of this code is like option 1 although I was expecting option 2

  • Your output does not match the code. And in general, the problem is described in such a way that nothing is clear at all. - rjhdby
  • Grouping can be done using the stack. - Oleksiy Morenets

0