Question

I have a class which can read a file, modify it an write it to another file. The characters in the output are correct , the only problem is that the lines need to have a length of 12 chars. How can I achieve this with my existing code?(I wrote a comment where in the code I want to do this)

My input file: http://gyazo.com/13fe791d24ef86e29ab6a6e89d0af609
The current output: http://gyazo.com/cc195c1d59a9d1fe3b4f2c54e71da8eb
The output I want : http://gyazo.com/04efcbb05c5d56b6e28972feb8c43fb8


String line;
StringBuilder buf = new StringBuilder();

public void readFile(){

    BufferedReader reader = null;

    try {
        File file = new File("C:/Users/Sybren/Desktop/Invoertestbestand1.txt");
        reader = new BufferedReader(new FileReader(file));


        //String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
            //buf.append(line);
            processInput();
        }


    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        };
    }
}
public void processInput(){

    buf.append(line);  

    if (buf.length()>7){
        buf.append("-");
    }


    /* if a * is followed by * change them to a !  */
    for (int index = 0; index < buf.length(); index++) {
        if (buf.charAt(index) == '*' && buf.charAt(index+1) == '*') {
            buf.setCharAt(index, '!');
            buf.deleteCharAt(index+1);
        }     

    } 
    // get last character from stringbuilder and delete      
    buf.deleteCharAt(buf.length()-1);

    /* start with a new line if the line length is bigger than 12 - how to do it? */
    //???

}


public void writeFile() {
    try {

        String content = buf.toString();

        File file = new File("C:/Users/Sybren/Desktop/uitvoer1.txt");

        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(content);
        bw.close();

        System.out.println("Done");


    } catch (IOException e) {
        e.printStackTrace();
    }
}}

No correct solution

OTHER TIPS

Would something along these lines help?

for (int i=13;i<buf.size();i+=13) {
  buf.insert(i, '\n');
  i++; // to account for the newline char just added
}

The numbers used may not be correct, either because of misunderstanding of the question or because it isn't tested.

for (int index = 0; index < buf.length(); index++) {
        if (buf.charAt(index) == '*' && buf.charAt(index+1) == '*') {
            buf.setCharAt(index, '!');
            buf.deleteCharAt(index+1);
        }    
} 

There will be an java.lang.ArrayIndexOutOfBoundsException at the end of the loop when you you call index+1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top