Search code examples
javajava.util.scannerdelimiter

Why do new lines keep appearing every time I append using "[,\n]" delimiter?


I'm new to java programming, and I currently build a program that updates an information from a user through file handling.

For instance, I have this initially saved in the notepad.

Milo,10,India
Jacob,15,California
Shan,7,France

I want to change the country. So I initialized a scanner to read every line in the file, with the delimiter ("[,\n]"). I was able to change it, but every time I do any changes, new lines appear every after each lines.

The output every time I append is like this: The new lines are appearing randomly, and I don't know why.

Milo,10,Italy
Jacob,15,California



Shan,7,France

Here is my code:

private static Scanner x;
    
    public static void main(String[] args) throws IOException {
        File file = new File("rewrite.txt");
        String nameString = "Milo";
        String newcountry = "Italy";
        
        String tempFile = "temp.txt";
        File oldFile = new File("rewrite.txt");
        File newFile = new File(tempFile);
        String name = ""; 
        String age = "";
        String country = "";
        
        try {
            FileWriter fw = new FileWriter(tempFile, true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            x = new Scanner(new File("rewrite.txt"));
            x.useDelimiter("[,\n]");
            
            while(x.hasNext()) {
                name = x.next();
                age = x.next();
                country = x.next();
                
                if (name.equals(nameString)) {
                    
                    pw.println(name + "," + age + "," + newcountry);
                    System.out.println(name + "," + age + "," + newcountry);
                
                } else {
                    pw.println(name + "," + age + "," + country);
                    System.out.println(name + "," + age + "," + country);
                }
            }
            x.close();
            pw.flush();
            pw.close();
            oldFile.delete();
            File dump = new File("rewrite.txt");
            newFile.renameTo(dump);
            
        } catch (Exception e) {
            // TODO: handle exception
        }

Solution

  • Read the whole line, split on the comma. Then assign the values to the variables and write to new file.

    public static void main(String[] args) {
        File input = new File("C:\\temp\\input.txt");
        String nameString = "Milo";
        String newcountry = "Italy";
    
        File output = new File("c:\\temp\\temp.txt");
        String name = "";
        String age = "";
        String country = "";
        try {
            FileWriter fw = new FileWriter(output, true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            Scanner x = new Scanner(input);
            while (x.hasNextLine()) {
                String line = x.nextLine();
                String[] split = line.split(",");
                name = split[0];
                age = split[1];
                country = split[2];
                if (name.equals(nameString)) {
                    pw.println(name + "," + age + "," + newcountry);
                    System.out.println(name + "," + age + "," + newcountry);
                } else {
                    pw.println(name + "," + age + "," + country);
                    System.out.println(name + "," + age + "," + country);
                }
            }
            x.close();
            pw.flush();
            pw.close();
    
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    

    Output is:

    Milo,10,Italy
    Jacob,15,California
    Shan,7,France
    

    without any empty lines.

    With some debugging, I found the issue, that you are experiencing. You only consume part of the new line delimiter:

    Text files created on DOS/Windows machines have different line endings than files created on Unix/Linux. DOS uses carriage return and line feed ("\r\n") as a line ending, which Unix uses just line feed ("\n")

    When updating the delimiter to also consume \r, it works just as well

    x.useDelimiter(",|\r\n");
    

    This tell the scanner to use , or \r\n as delimiter which consumes the the windows new line characters