Question: I'm coding in Java and I want to split my string. I want to split it at. /* sort */ Yes I plan to split

I'm coding in Java and I want to split my string. I want to split it at.

/* sort */ 

Yes I plan to split a .java file that I have read as a string so I need it to include "/* sort */". I'm creating a code that sorts Arrays that are predefined in java class file.

Exactly that and do another split at

} 

and then I wanted help how to go about splitting up the array since I'll be left with

an example would be this

final static String[] ANIMALS = new String[] /* sort */ { "eland", "antelope", "hippopotamus"}; 

My goal would be to sort that Array inside a .java file and replace it. This is my current code

 private void editFile() throws IOException { //Loads the whole Text or java file into a String try (BufferedReader br = new BufferedReader(new FileReader(fileChoice()))) { StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(line); sb.append(System.lineSeparator()); line = br.readLine(); } everything = sb.toString(); } arrayCutOff = everything.split("////* sort *////"); for(int i = 0; i < arrayCutOff.length; i++){ System.out.println(arrayCutOff[i]); } } 

This basically reads the whole .txt or .java file completely with the exact same formatting into one string. I planned to split it at /* sort */ and sort the array inside but I realized if I did that I probably can't replace it.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!