Results 1 to 8 of 8
Thread: Deleting from an object
- 08-08-2008, 12:41 AM #1
Member
- Join Date
- Aug 2008
- Posts
- 5
- Rep Power
- 0
Deleting from an object
Hi it's me again, I've pretty much finished a program I'm making, which does the following:
The program is for a garage, and it does the following functions:
Adds a new car
Display cars
Amend price of a car
Delete car ('sell' car)
Find a certain car
Almost everything is working perfect, except that I can't get the 'delete' function working correctly.
All the cars data is stored in a text file, like so:
When the program is loaded, the text file data is converted into objects so I can manipulate them. When manipulation is finished, the data is written back to the text file.C04ABC
BMW
X219
2004
40000
C04XYZ
Honda
Civ
2004
10000
L17LLL
Rover
MGF1.8
1998
4000
V40GOR
Vauxhall
Omega 2
1998
1000
R45GER
BMW
Ver2
1995
1000
B34FJF
Honda
Civic
1992
500
****
For some reason, my 'delete' functions only works if you delete the last entry in the text file, any other and I get a NullPointerException error...Any ideas? Here is my code for the sellCar method.
And here is the method that saves the objects back to the text file, the line the NullPointerException points to the getRegNo(); line.Java Code:public void sellCar() throws IOException { if (activeCars != 0) { tempRegNo = Text.readString("Please enter the registration number of the car you wish to sell."); while(search < activeCars && found == false){ if(carDetails[search].getRegNo().equalsIgnoreCase(tempRegNo)) { found = true; } else { search++; } } if (found == true) { correct = Text.readChar("Here are the details of the car to be deleted: \n" + "Manufacturer: " + carDetails[search].getManufacturer() + "\n" + "Model: " + carDetails[search].getModel() + "\n" + "Registration No: " + carDetails[search].getRegNo() + "\n" + "Year: " + carDetails[search].getYear() + "\n" + "Price: £" + carDetails[search].getPrice() + "\n" + "\n" + "Are these details correct? If so, press Y or y"); if (correct == 'Y' || correct == 'y') { carDetails[search] = null; activeCars--; System.out.println("Deleted."); } else { found = false; } } } else { Text.showMessage("There are no car details in the database."); } correct = '?'; tempRegNo = null; tempManufacturer = null; tempModel = null; tempPrice = 0; tempYear = 0; }
Many thanks :)Java Code:public void saveCarDetails() throws IOException { { PrintWriter outputFile; outputFile = Text.create ( textFilesPath + "car details.txt" ); for ( int printcount = 0; printcount < activeCars; printcount++ ) { outputFile.println ( carDetails[printcount].getRegNo() ); outputFile.println ( carDetails[printcount].getManufacturer() ); outputFile.println ( carDetails[printcount].getModel() ); outputFile.println ( carDetails[printcount].getYear() ); outputFile.println ( carDetails[printcount].getPrice() ); outputFile.println (""); }//end-for outputFile.println ("****"); outputFile.close(); } }
- 08-08-2008, 02:44 AM #2
Looks like there is no element: carDetails[printcount]. ie that element in the array is null.
Are you sure activeCars has the correct value?
Does the carDetails array have any empty spots? If it does, you need to test for them.
Add println() statements to your code to show the values as you fill the array, etc
- 08-10-2008, 01:47 AM #3
Member
- Join Date
- Aug 2008
- Posts
- 5
- Rep Power
- 0
Thanks for the reply, I seem to got a bit further now - I realise I need to move back cars in positions after the deleted car, so it fills the gap. However I'm stumped on how to do this..I currently have this code:
That does delete the position, however all that seems to do is duplicate the previous entry over the deleted one.Java Code:carDetails[search] = null; for (int move = search+1; move <= MAX_CARS; move++ ) { carDetails[search] = carDetails[search-1]; } activeCars--; Text.showMessage("The car has now been sold.");
The 'search' position in line 1 is the position of the file to be deleted, and is found using a search before that.
Max cars is a final variable I defined previously.
And activeCars is the number of cars in the database.
Any ideas?:)
Thanks :)
- 08-10-2008, 02:19 AM #4
There are better containers to hold data when you need to add/delete elements from it. Arrays are sort of static and require a lot of programing if you are going to delete elements or add elements past the array's capacity. Look at using another container type such as ArrayList or some of the other classes in the Collection framework (a fancy word for a bunch of classes that are containers and can hold other objects in different manners and relationships).
Otherwise, to be able to "delete" an entry from your array make a special class(that extends the normal class) to put in your array that says its a deleted entry and then test for it in your processing loop and skip it.
- 08-10-2008, 02:30 AM #5
Member
- Join Date
- Aug 2008
- Posts
- 5
- Rep Power
- 0
Thanks for the suggestion, yeah after researching ArrayList it does seem like a great solution, a lot more versatile than objects...However this is for a piece of coursework, and we've been told we must use Java objects to accomplish this - Which is rather annoying!
Thanks anyway :)
- 08-10-2008, 03:07 AM #6
Member
- Join Date
- Aug 2008
- Posts
- 5
- Rep Power
- 0
Just editted this out as I've realised something...
As soon as the car is deleted, it's parsed into a text file, where blank lines are completely ignored...
So I think what I need to do is set every field to just blank of the position I'm deleting to blank - i.e. a space. However 2 of my fields are integers, and I can't set them to blank :( is there a way to set an integer to just, blank space somehow? ThanksLast edited by vitaminz; 08-10-2008 at 03:40 AM.
- 08-10-2008, 03:42 AM #7
Not sure what you mean by that. Java has primitives like int, boolean, double, byte ... everything else is an object.we must use Java objects
Add a flag to the objects that are in the array that says the element is deleted.
Unless, of course, if the assignment is to write code to move the contents of the array around. The way I've used to design this kind of code is to use a paper and pencil, draw a list, set pointers and work out how to move contents and pointers to get the desired result.
- 08-10-2008, 03:56 AM #8
Member
- Join Date
- Aug 2008
- Posts
- 5
- Rep Power
- 0
Ah sorry I get a tad confused with the terminology. What I mean is, the array is created like this:
and a new car is made by using something like this:Java Code:private Car [] carDetails = new Car[MAX_CARS];
The Car object is referenced in a different class, Car.javaJava Code:carDetails[activeCars] = new Car( tempRegNo, tempManufacturer, tempModel, tempYear, tempPrice );
Hope I'm not too confusing here lol, thanksJava Code:public class Car { private String manufacturer; private String model; private String regNo; private int year; private int price; public Car(String regNo, String manufacturer, String model, int year, int price) { this.regNo = regNo; this.manufacturer = manufacturer; this.model = model; this.year = year; this.price = price; }
Similar Threads
-
Deleting Contractions
By theonly in forum New To JavaReplies: 3Last Post: 04-27-2008, 07:44 PM -
Deleting All rows in the JTable
By surot in forum New To JavaReplies: 1Last Post: 04-16-2008, 10:44 AM -
Deleting a File that is opened
By ravian in forum Advanced JavaReplies: 6Last Post: 01-30-2008, 02:05 PM -
Deleting an empty directory
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 07:17 AM -
deleting elements
By nalinda in forum New To JavaReplies: 2Last Post: 12-06-2007, 01:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks