Search code examples
javadatabasecursorcursor-position

Adding information to a txt file - Issue with cursor


So I'm trying to add text to a txt file, I have succeeded in doing this, the user is prompted about what they would like to add and it is included when I open the txt file after running my program.

Now, here's my problem: The first time I try to add something to the txt file, it creates a blank row and under that it will have the information the user has added, the reason for this is because when Java opens my file, the typing cursor is on the bottom of the last line, where there is no space and it is ready to enter information, like this:

enter image description here

Alright, cool, so this means whenever I add anything to the txt file from my Java program, it will automatically be on a new line and I will not need to add any println spacing to enter a new line. Now here's where another problem concurs, after Java puts the new text into the txt file, it leaves the typing cursor at the END of the new line added, not at the new line under it where there is no data. So this means if I try to add new information twice, it will appear on the same line and not a new one. So to fix this, I simply added a

pw.println();

statement at the start of my print writer to ensure it puts the cursor on the new line. But now, when i try to add data for the first time again after adding that last line of code, it will create a empty space between the last line of data and the new line of data I have added. Like so:

enter image description here Now, whenever the user wants to search through the database, because I have null checks in my searches, whenever the program comes across that empty line between my original data and the new data that has been added, It will not continue the search, therefore any information that the user has added to the database is rendered useless and Java won't be able to search for it because of the empty line that separates it.

Basically, either I keep the pw.println() and have all my data come up but have a space inbetween the original data so It can't be searched for, OR i can remove it and then all new data that is added appears on the same line, which is not what I want.

This is the code for adding data to the txt file:

    public static void add() throws IOException
{
    String filename="Elements.txt";
    PrintWriter pw = new PrintWriter(new FileWriter(filename,true));
    pw.println();
    String element=JOptionPane.showInputDialog(null, "Enter name of element.", "Add an Element", JOptionPane.INFORMATION_MESSAGE);;
    pw.write(element + ",");

    String symbol=JOptionPane.showInputDialog(null, "Enter symbol of element.", "Add an Element", JOptionPane.INFORMATION_MESSAGE);;
    pw.write(symbol + ",");

    String atomicNumber=JOptionPane.showInputDialog(null, "Enter atomic number.", "Add an Element", JOptionPane.INFORMATION_MESSAGE);;
    pw.write(atomicNumber + ",");

    String atomicMass=JOptionPane.showInputDialog(null, "Enter atomic mass", "Add an Element", JOptionPane.INFORMATION_MESSAGE);;
    pw.write(atomicMass + ",");

    String valence=JOptionPane.showInputDialog(null, "Enter # of valence electrons.", "Add an Element", JOptionPane.INFORMATION_MESSAGE);;
    pw.write(valence);

    pw.close();

}

Is there any way I can manipulate the cursor on the txt file to ensure that all new data added shows up properly so the user can search for it afterwards? Opening up the txt file and placing the cursor where I want to didn't work.


Solution

  • The idea is to do println() (which adds a line end) at the end of writing whatever should be on a line. If you println() before any other data, it will very likely create an empty line (unless the last line is without a line end).

    String filename="Elements.txt";
    PrintWriter pw = new PrintWriter(new FileWriter(filename,true));
    
    String element= ... ;
    pw.write(element + ",");
    
    // ... more ...
    
    String valence=...;
    pw.write(valence);
    
    pw.println();
    
    pw.close();