Reputation: 1400
I am trying to write a Java program that will append and prepend html code to text. The idea is to have a text file with some content that is delimited by &&
and append/prepend the text with the appropriate html tags. Specifically, I am trying to automate html generation of bootstrap columns. I ask the user for a file and then read it into an arraylist. If the program is working correctly each spot in the array list should contain the text between the delimiter &&
How can I write an edit method
that will take the array list as the parameter and add the appropriate tags and return a new array list that I can pass to a write method?
**edit: I should mention append and prepend methods are just placemarkers for what I want to write to each text delimited entry. Open to any suggestions on how to write the method.
public static ArrayList<String> readLines() throws IOException {
ArrayList<String> lines = new ArrayList<>();
JOptionPane.showMessageDialog(null, "Please choose a file");
JFileChooser input = new JFileChooser();
int a = input.showOpenDialog(null);
String file = "";
if (a == JFileChooser.APPROVE_OPTION) {
File selectedFile = input.getSelectedFile();
file = selectedFile.getPath();
}
//use file input to read in line one at a time
Scanner read = new Scanner(new File(file));
read.useDelimiter("&&");
while(read.hasNext()){
lines.add(read.next());
}
return lines;
}
public static void editFile(ArrayList<String> formalLines){
//appendText() --> insert content ---> prependText() --> return arrayList to write to file
}
public static void appendText () {
System.out.println("<div id=\"page-content-wrapper\">");
System.out.println(" <div class=\"container-fluid\">");
System.out.println(" <div class=\"col-xs-6 col-md-4\">");
}
public static void prependText(){
System.out.println(" </div>");
System.out.println(" </div>");
System.out.println("</div>");
}
public static void writeFile(ArrayList<String> formalFinalArray) throws FileNotFoundException, UnsupportedEncodingException {
try {
FileOutputStream fos = new FileOutputStream("output");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(formalFinalArray); // write MenuArray to ObjectOutputStream
oos.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
Upvotes: 0
Views: 2355
Reputation: 3568
This is fairly simple. If I understand correctly, you'd like the strings you have with your ArrayList strings in the middle.
So we have a line by line ArrayList
of strings in our file? That makes it easy, as we can access line number by index. So to prepend we can will start with your last string in the prepend set of strings (because we're adding to index 0 and letting them move up the list naturally). So we will:
void prependText() {
lines.add(0, " <div class=\"col-xs-6 col-md-4\">");
lines.add(0, " <div class=\"container-fluid\">");
lines.add(0, "<div id=\"page-content-wrapper\">");
}
void appendText() {
lines.add(" </div>");
lines.add(" </div>");
lines.add("</div>");
}
Instead, if you'd like every item in lines
to be sandwiched so to speak, then we'll:
ArrayList<String> newList = new ArrayList<>();
for(int i = 0; i < lines.size(); i++) {
newList.add("<div id=\"page-content-wrapper\">" +
" <div class=\"container-fluid\">" +
"<div id=\"page-content-wrapper\">" +
lines.get(i) +
" </div>" +
" </div>" +
"</div");
}
Insert '\n's as needed at the ends each string if you need newlines.
Finally, newList
is—you guessed it—your new list.
Upvotes: 1
Reputation: 6006
Use StringBuilder
StringBuilder lines = new StringBuilder();
Add the starting tags to it then where you are doing this
lines.add(read.next);
Do this instead
String tagStart = "<div>";
String tagEnd = "</div>";
lines.append(tagStart).append(read.next).append(tagEnd);
After the loop you can add the end tags just like you put for each line in the loop.
At the end when you want to write it do
lines.toString();
Hope this is helpful.
You can write 'getPrependText()' method to get startTag
String startTag = getPrependTex();
and getPrependText(); will return you the desired tags to prepend.
Upvotes: 1
Reputation: 2065
You can make sure append and prepend methods also return ArrayLists and the combine all 3 ArrayLists (inside the editFile() method) using the addAll method eg: http://beginnersbook.com/2013/12/how-to-joincombine-two-arraylists-in-java/
Im guessing this is what you want :
public static void editFile(ArrayList<String> formalLines){
//appendText() --> insert content ---> prependText() --> return arrayList to write to file
ArrayList<String> newList = new ArrayList<String>();
try {
for(String item:readLines()){
newList.addAll(appendText());
newList.add(item);
newList.addAll(prependText());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static ArrayList<String> appendText () {
ArrayList<String> append = new ArrayList<String>();
append.add("<div id=\"page-content-wrapper\">");
append.add(" <div class=\"container-fluid\">");
append.add(" <div class=\"col-xs-6 col-md-4\">");
return append;
}
public static ArrayList<String> prependText(){
ArrayList<String> prepend = new ArrayList<String>();
prepend.add(" </div>");
prepend.add(" </div>");
prepend.add("</div>");
return prepend;
}
Upvotes: 2