473,473 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

java.util.NoSuchElementException

1 New Member
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.Scanner;

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.createNewFile();
}
else{
Scanner scan = new Scanner(new FileInputStream(file));
while(scan.hasNext()) {
scan.nextLine();
numberEntries ++;
}
}

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

filename = newName;
File tempFile = new File(newName);
if(!tempFile.exists()){
tempFile.createNewFile();
}
file = tempFile;
}
public int getNumEntries(){
return numberEntries;
}
public void setNumEntries(int newNumEntries){
numberEntries = newNumEntries;
}
public void addEntry(Entry E){

PrintWriter pw = null;
try {
pw = new PrintWriter(new FileOutputStream(file, true));
}
catch(FileNotFoundException e){
System.out.println("Could not open file");
}
pw.println(E);
pw.close();

numberEntries ++;
}

public Entry[] fileToArray() throws IOException{
Entry[] entryArray = new Entry[this.getNumEntries()];
Scanner scan = null;
try{
scan = new Scanner(new FileInputStream(file));
}
catch(FileNotFoundException e){
System.out.println("Could not open file.");
}
scan.useDelimiter(" , |\n");
for (int i = 0; i < this.getNumEntries(); i++){
entryArray[i] = new Entry(scan.next(),scan.next(),scan.next());
}
return entryArray;
}

public void deleteEntry(Entry toBeDeleted) throws IOException{
Entry[] fileArray = this.fileToArray();
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileOutputStream(file));
}
catch(FileNotFoundException e){
System.out.println("Could not open file");
}

for (int i = 0; i < fileArray.length; i++){
if (!fileArray[i].equals(toBeDeleted)){
pw.println(fileArray[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("database.txt");

database.addEntry(entry);
database.addEntry(e1);
Entry[] a = database.fileToArray();
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
Entry delete = new Entry("*", "*", "*");
database.deleteEntry(delete);

}

}
Dec 6 '11 #1
0 1827

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Geetu | last post by:
I am trying to jar up a file and getting this exception, not sure what could be wrong. The same piece of code works fine in few machines but there is one machine where this piece of code gives this...
4
by: 3rdshiftcoder | last post by:
Hi- I'd like to use this class from a book but am getting a compiler error. On the line private List < String > list; p.932 Chapter 19 found in the code below in the eclipse editor it is...
0
by: daniel li | last post by:
I got another convert issue. This is the same project as I posted yesterday, and received a perfect response. The conversion threw a SupportClass for me and one of the data type is...
4
by: puntino | last post by:
Hi I have created my Alarm, at compile time I don't have any problem, but at run time I receive the follo wing messages: Exception in thread "main" java.lang.NullPointerException at...
3
by: Dameon99 | last post by:
Hi.. Im experiencing a weird error I dont know how to fix. I have a scanner object and whenever I use nextInt, anything above 3 causes the program to crash saying and links me to this line of the...
4
by: HaifaCarina | last post by:
here's the complete lines of errors.. Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java:332) at...
6
oll3i
by: oll3i | last post by:
when i run my jsp file i get an error SEVERE: Servlet.service() for servlet jsp threw exception java.util.MissingResourceException: Can't find bundle for base name MessagesBundle, locale pl_PL...
4
by: sukatoa | last post by:
This was my first time to encouter this kind of exception.... that exception appears when i invoked the the method below. private final String encrypting(String enc){ int...
1
by: jimgym1989 | last post by:
I dont get it..why is the error: Exception in thread "main" java.util.InputMismatchException this is my code /** * @(#)textFileRead.java * * * @author * @version 1.00 2008/10/17 */
6
by: monkey0525 | last post by:
Each time I run the ProgTwo program, it displays: 1. Display one product 2. Display all products 3. Add a new CD 6. Exit Enter your choice: Exception in thread "main"...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.