473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Setting an object to blank?

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

When the program is loaded, it attempts to read existing car data from a text file, which is then converted into an object array. Then when manipulation is finished with the details, it's all completely written back to the text file (Overwriting it, not appending to it).

Almost everything is working perfect, except that I can't get the 'delete' function working correctly. I have 5 fields in my object array, these are Registration Number, Manufacturer, Model (all 3 are strings), and year and price (which are ints). I've researched this and actually deleting from an array is nigh on impossible, but I realised that when data is written back to the text file straight after a car is sold, it skips all blank lines, so if I convert a car position's fields to blank, it'll do what I want it do...


Problem is, I need to somehow set the int to be 'blank'...not 0, just like a space. Is this possible? Thanks
Aug 10 '08 #1
5 1419
Dököll
2,364 Expert 2GB
Heiya!

Can you post the delete function?

It sounds like you writing to a text file in two instances. Why not simply write to it once?

Meaning that, if data are already being written to a text file, why not do your manipulation upon submitting to that text file firsthand. You would not need to overwrite...

Still, please post what you have working, you;re sure to get sound support here.

In a bit!
Aug 10 '08 #2
Thanks for the reply :)

Maybe I should explain about my program first, sorry last night I didn't really explain much.

The first thing that happens when the program is run, is the car details text file is loaded and convert into an object array. The coding used to do this is (the text file is opened with a BufferedReader previously, but I thought copying that code would be a bit pointless)

Expand|Select|Wrap|Line Numbers
  1.        activeCars = 0;
  2.        aRegNo = Text.readLine ( inputCarFile );
  3.  
  4.        while ( aRegNo.equals ( "****" ) == false )
  5.        {
  6.  
  7.             aManufacturer = Text.readLine ( inputCarFile );
  8.             aModel = Text.readLine ( inputCarFile );
  9.             aYear = Text.readInt ( inputCarFile );
  10.             aPrice = Text.readInt ( inputCarFile );
  11.  
  12.             carDetails[activeCars] = new Car( aRegNo, aManufacturer, aModel, aYear, aPrice );
  13.  
  14.             activeCars++;
  15.             System.out.println("");
  16.  
  17.             aRegNo = Text.readString ( inputCarFile );
  18.  
  19.       }
  20.  
  21.       inputCarFile.close();
  22.  
Then when adding a car to the database, something like this is used to create the object instance:

Expand|Select|Wrap|Line Numbers
  1.                 carDetails[activeCars] = new Car( tempRegNo, tempManufacturer, tempModel, tempYear, tempPrice );
  2.                 activeCars++;
And this is what I'm currently using to sell a car from the database (line 11 is not valid, that's just me testing around on how to set all the fields to blank)


Expand|Select|Wrap|Line Numbers
  1.             correct = Text.readChar("Here are the details of the car to be sold: \n" +
  2.             "Manufacturer: " + carDetails[search].getManufacturer() + "\n" +
  3.             "Model: " + carDetails[search].getModel() + "\n" +
  4.             "Registration No: " + carDetails[search].getRegNo() + "\n" +
  5.             "Year: " + carDetails[search].getYear() + "\n" +
  6.             "Price: £" + carDetails[search].getPrice() + "\n" +
  7.             "\n Are these details correct? If so, press Y or y");
  8.  
  9.             if (correct == 'Y' || correct == 'y') 
  10.             {
  11.                 carDetails[search] = "";
  12.  
  13.                 activeCars--;
  14.                 Text.showMessage("The car has now been sold.");
  15.  
  16.             }
  17.  
After each manipulation, the saveCarDetails method is called:

Expand|Select|Wrap|Line Numbers
  1.          for ( int printcount = 0; printcount < activeCars; printcount++ )
  2.          {
  3.  
  4.             outputFile.println ( carDetails[printcount].getRegNo() );
  5.             outputFile.println ( carDetails[printcount].getManufacturer() );
  6.             outputFile.println ( carDetails[printcount].getModel() );
  7.             outputFile.println ( carDetails[printcount].getYear() );
  8.             outputFile.println ( carDetails[printcount].getPrice() );
  9.             outputFile.println ("");
  10.          }
  11.          outputFile.println ("****");
  12.  
The car constructor is defined in a file called Car.java, and looks like this:

Expand|Select|Wrap|Line Numbers
  1. public class Car 
  2. {
  3.   // attributes
  4.  
  5.   private String manufacturer;
  6.   private String model;
  7.   private String regNo;
  8.   private int    year;
  9.   private int    price;
  10.  
  11.   // constructor
  12.  
  13.   public Car(String regNo, String manufacturer, String model, int year, int price)
  14.   {
  15.     this.regNo = regNo;
  16.     this.manufacturer = manufacturer;
  17.     this.model = model;
  18.     this.year = year;
  19.     this.price = price;
  20.   }
  21.  
The following is mutator methods for cars, which I can call to.

Expand|Select|Wrap|Line Numbers
  1.   public void setManufacturer(String manufacturer)
  2.   {
  3.       this.manufacturer = manufacturer;
  4.   }
  5.  
  6.   public void setModel(String model)
  7.   {
  8.     this.model = model;
  9.   }
  10.  
  11.   public void setRegNo(String regNo)
  12.   {
  13.     this.regNo = regNo;
  14.   }
  15.  
  16.   public void setYear(int year)
  17.   {
  18.     this.year = year;
  19.   }
  20.  
  21.   public void setPrice(int price)
  22.   {
  23.     this.price = price;
  24.   }
  25.  
  26.   public String getManufacturer()
  27.   {
  28.     return this.manufacturer;
  29.   }
  30.  
  31.   // accessor methods
  32.  
  33.   public String getModel()
  34.   {
  35.     return this.model;
  36.   }
  37.  
  38.   public String getRegNo()
  39.   {
  40.     return this.regNo;
  41.   }
  42.  
  43.   public int getYear()
  44.   {
  45.     return this.year;
  46.   }
  47.  
  48.   public int getPrice()
  49.   {
  50.     return this.price;
  51.   }
  52.  
  53.  
  54.   public String toString()
  55.   {
  56.     return "            \nManufacturer        : " + manufacturer + 
  57.                        "\nModel               : " + model + 
  58.                        "\nRegistration number : " + regNo +
  59.                        "\nYear                : " + year +
  60.                        "\nPrice               : " + price;
  61.   }
  62.  
Hopefully that makes sense...:) thanks again
Aug 10 '08 #3
JosAH
11,448 Expert 8TB
You have an array of Cars; when you sell a Car you want to get rid of it; why not
simply set the reference in the array to null? That way you'll end up with an array
of Cars and nulls so you have to check for that null value every time you loop
over that array (the null represents a sold car).

There are better ways to do it (e.g. use an ArrayList instead) but the above
approach works as well but you have to change all your loops a bit.

Or: add a boolean field 'sold' to your Car class and set it to true when the car is
sold; you have to anticipate for that in your loops as well.

kind regards,

Jos
Aug 10 '08 #4
Ahhh, thanks, that seems to work great :) I've made it set the sold cars fields to 0, then had my save car method skip over any lines with just 0.

Thanks again!
Aug 10 '08 #5
JosAH
11,448 Expert 8TB
Ahhh, thanks, that seems to work great :) I've made it set the sold cars fields to 0, then had my save car method skip over any lines with just 0.

Thanks again!
Yes, that is the Fortran/Pascal/Cobol way of doing it (all those sily 9999 values).
Have a look at the ArrayList class; it acts like an array but you can remove
entries from it, i.e. there is no need to put sentinel values in that entry.

kind regards,

Jos
Aug 10 '08 #6

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

Similar topics

5
by: Laura | last post by:
Hello, Newbie question: Does anyone know how to dynamically set the screen width in an applet? I have an applet that creates a horizontal bar menu on a webpage, and I would like the width to be...
16
by: cwizard | last post by:
I'm calling on a function from within this form, and there are values set but every time it gets called I get slammed with a run time error... document.frmKitAmount.txtTotalKitValue is null or not...
3
by: Lalit Bhatia | last post by:
how can we set mouse pointer in a class (not in form ) in C# ? Lalit
2
by: Dan | last post by:
I'd like to display some HTML code in a form using the axwebbrowser control, without having to create a temporary file to navigate to. I'm trying the following code (let's say that the web browser...
1
by: Frank Yamrick | last post by:
I am trying to program an application that requires a large number of screnes that are very similar in the respect that all the bottons and labels interact with each other with the same...
5
by: eBob.com | last post by:
I am trying to change some Structures to Classes. The Classes now look like this ... Public Class OneAttr Public AttrName As String Public Column As String Public Caption As String End Class...
5
by: Rotsey | last post by:
Hi, I have a combobox that when I set the SelectedIndex to -1 it sets to 0. The combobox Items property says there is 20 items in it. Anyone know why this could be please? rotsey
5
by: RyanN | last post by:
Hello, I'm trying to teach myself OOP to do a data project involving hierarchical data structures. I've come up with an analogy for testing involving objects for continents, countries, and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.