java.util.NoSuchElementException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tou Vue
    New Member
    • Dec 2011
    • 1

    java.util.NoSuchElementException

    I am writing a simple database class and it works fine the first time but then i get this error message the times after. If i delete the text file that it is writing to then it is fine. Please help I am lost at this point.

    import java.io.*;
    import java.util.Scann er;

    public class Database {

    private String filename;
    private File file;
    private int numberEntries;

    public Database(String name) throws IOException{
    numberEntries = 0;
    filename = name;
    file = new File(name);
    if (!file.exists() ){
    file.createNewF ile();
    }
    else{
    Scanner scan = new Scanner(new FileInputStream (file));
    while(scan.hasN ext()) {
    scan.nextLine() ;
    numberEntries ++;
    }
    }

    }
    public String getDatabaseName (){
    return filename;
    }
    public void setDatabaseName (String newName) throws IOException{

    filename = newName;
    File tempFile = new File(newName);
    if(!tempFile.ex ists()){
    tempFile.create NewFile();
    }
    file = tempFile;
    }
    public int getNumEntries() {
    return numberEntries;
    }
    public void setNumEntries(i nt newNumEntries){
    numberEntries = newNumEntries;
    }
    public void addEntry(Entry E){

    PrintWriter pw = null;
    try {
    pw = new PrintWriter(new FileOutputStrea m(file, true));
    }
    catch(FileNotFo undException e){
    System.out.prin tln("Could not open file");
    }
    pw.println(E);
    pw.close();

    numberEntries ++;
    }

    public Entry[] fileToArray() throws IOException{
    Entry[] entryArray = new Entry[this.getNumEntr ies()];
    Scanner scan = null;
    try{
    scan = new Scanner(new FileInputStream (file));
    }
    catch(FileNotFo undException e){
    System.out.prin tln("Could not open file.");
    }
    scan.useDelimit er(" , |\n");
    for (int i = 0; i < this.getNumEntr ies(); i++){
    entryArray[i] = new Entry(scan.next (),scan.next(), scan.next());
    }
    return entryArray;
    }

    public void deleteEntry(Ent ry toBeDeleted) throws IOException{
    Entry[] fileArray = this.fileToArra y();
    PrintWriter pw = null;
    try {
    pw = new PrintWriter(new FileOutputStrea m(file));
    }
    catch(FileNotFo undException e){
    System.out.prin tln("Could not open file");
    }

    for (int i = 0; i < fileArray.lengt h; i++){
    if (!fileArray[i].equals(toBeDel eted)){
    pw.println(file Array[i]);
    }
    }
    pw.close();
    }

    public static void main(String[] args) throws IOException{
    Entry entry = new Entry("Toy Story 3", "2010", "G");
    Entry e1 = new Entry("The Matrix", "1999","R") ;
    Database database = new Database("datab ase.txt");

    database.addEnt ry(entry);
    database.addEnt ry(e1);
    Entry[] a = database.fileTo Array();
    for (int i = 0; i < a.length; i++) {
    System.out.prin tln(a[i]);
    }
    Entry delete = new Entry("*", "*", "*");
    database.delete Entry(delete);

    }

    }
Working...