user1712638
user1712638

Reputation: 317

failing to save data in a text file

I'm trying to make a class that takes info from the GUI this saves it to a text file which I use as my "Database" but for some reason the PrintWriter object doesn't write the new data in the file. here's my code

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class IO {

    File f = new File("DB.txt");
    PrintWriter write;
    Scanner input;
    String[][] data;
    String nameToSearch;

    // search constructor
    public IO(String name) {
        super();
        nameToSearch = name;
        try {
            input = new Scanner(f);
        } catch (FileNotFoundException e) {
            System.out.println("File not found please restart the program");
        }
        data = new String[linesCounter()][2];
        for (int i = 0; i < linesCounter(); i++) {
            data[i][0] = input.nextLine();
            data[i][1] = input.nextLine();
        }
    }

    public IO(String name, String number) {
        try {
            write = new PrintWriter(new FileWriter(f, true));
        } catch (IOException e) {
            System.out.println("Error");
        }
        write.println(name);
        write.println(number);
    }

    int linesCounter() {
        try {
            input = new Scanner(f);
        } catch (FileNotFoundException e) {
            System.out.println("File not found please restart the program");
        }
        int counter = 0;
        while (input.hasNext()) {
            input.nextLine();
            counter++;
        }
        return counter / 2;
    }

    int contactFinder() {
        for (int i = 0; i < linesCounter(); i++)
            if (data[i][0].equalsIgnoreCase(nameToSearch))
                return i;
        return -1;
    }

    String nameGetter() {
        return data[contactFinder()][0];
    }

    String numGetter() {
        return data[contactFinder()][1];
    }

}

Upvotes: 0

Views: 1569

Answers (2)

PermGenError
PermGenError

Reputation: 46428

you need to close the printwriter after you have finished writing into the file using printwriter.close()

  try {
            write = new PrintWriter(new FileWriter(f, true));
              write.println(name);
              write.println(number);
              write.close();
        } catch (IOException e) {
            System.out.println("Error");
        }

    }

EDIT: For your NoSuchElement Excepion, you should check if there is a nextline in the file before invoking Scanner.nextline() using Scanner.hasNextLine().

 for (int i = 0; i < linesCounter(); i++) {
      if(input.hasNextLine()){
        data[i][0] = input.nextLine();
        data[i][3] = input.nextLine();
    }
   }

Upvotes: 4

Matt Klooster
Matt Klooster

Reputation: 767

It's possible the PrintWriter never got flushed. You can do this manually with

write.flush();

That will ensure the buffer gets written to the file.

Upvotes: 0

Related Questions