I've got to make a Property To Let program that can allow the user to add, view or delete property details to and from a text file. I have done the add and view part but i am having trouble getting the delete working.

I've managed to get it to delete the last line of a text file by deleting a set amount of characters from the end. I've also been told by a friend that it involves removing all contents of that text file into a vector (yes i have to use vectors) and then deleting the specific line from there. but when i tried this it could never find the line in the file even though it was there.

My project has three relative classes, i'll display the code for these below. I'm also fairly new to java so forgive me if i need things in simple terms

Recommended Answers

All 7 Replies

This is the main code

import java.io.*;
import java.util.Vector;
import java.io.File;
import java.io.IOException;

public class PropertyRental

{

    public static Vector v = new Vector();
    public static String menu;
    public static String[] addy;
    public static String pc, hn, mr, ua, readstr;
    public static int i, ln;
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static rff Read = new rff();

    public static void DataR()
    {
        readstr = Read.readFromText();
        String[] splitArray = null;
        splitArray = readstr.split(",");
        for(int i=0;i<splitArray.length;i++)
        {
        if (v.size()<splitArray.length)
            {
                v.addElement(new PropertyToLet(splitArray[0], splitArray[1], splitArray[2]))  ;  
            }
        }
      }
    
    public static void main()

    {
        
        try
        {    
            
            do
            {
            System.out.println("Welcome to your local Property Rental Company");
            System.out.println("You now have the following options");
            System.out.println("");
            System.out.println("1. Add a Property");
            System.out.println("2. Remove a Property");
            System.out.println("3. Display all Properties");
            System.out.println("4. Exit the Program");
            System.out.println("");
            i = Double.valueOf(br.readLine()).intValue();
            System.out.println("");
           
            if(i == 1)
            {
                //BufferedWriter appends to properties.txt
                //Our user input buffer
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("--- Add a Property ---");

                

              

                    //User enters address
                    System.out.println("\nEnter postcode (must be written as pe158nb)");
                    pc = br.readLine();
                    //If the user has entered 'q' to quit, leave this loop
                    System.out.println("Enter house number");
                    hn = br.readLine();
                    System.out.println("Enter monthly rent");
                    mr = br.readLine();
                    //Write our details to file
                    v.addElement(new PropertyToLet(pc, hn, mr));
                
                    PrintWriter outFile;
                    try
                    {
                        outFile = new PrintWriter(new FileWriter("properties.txt" ,true));
                        PropertyToLet obj = (PropertyToLet) (v.lastElement());
                        outFile.println(obj.getpc() + "," + obj.gethn() + "," + obj.getmr());      
                        outFile.close();
                    }

                    catch(IOException IOE)
                    {
                        System.err.println(IOE + "Has occured");
                    }         
                }

            if(i == 2)
            {
               try
               
               {                
                   System.out.println("\nEnter the line number of the property you wish to remove");
                   int ln = Integer.valueOf(br.readLine()).intValue()-1;
                   BufferedReader br=new BufferedReader(new FileReader("properties.txt"));
                   String line=null;
                   while ((line=br.readLine()) != null) 
                   {
                        addy = line.split(",");
                        PropertyToLet ptl=new PropertyToLet(addy[0],addy[1],addy[2]);
                        v.addElement(ptl);
                   }
                   v.removeElementAt(ln);                   
                   PrintWriter outFile = new PrintWriter(new FileWriter("properties.txt"));
                   for (int i=0; i<v.size(); i++) 
                   {
                        PropertyToLet obj = (PropertyToLet) (v.lastElement());
                        outFile.println(obj.getpc() + "," + obj.gethn() + "," + obj.getmr());      
                   }
                   outFile.close();
                }               
                catch(ArrayIndexOutOfBoundsException aioobe)
                {
                    System.out.println("An Error Has occurred");   
                }
                catch(IOException ex)
                {
                    ex.printStackTrace();
                }
                
            }          
            if(i == 3)
            {             
              {
                  String line="";
                  String data="";
                  try
                  { 
                      BufferedReader br = new BufferedReader(new FileReader("properties.txt"));
                      line = br.readLine();                      
                      int counter=0;
                      do// EOF condition
                      {
                          counter++;
                          System.out.println(counter + ": " + line);
                          data = data + " \n" + line;
                          //line = br.readLine();                                  
                      }

                      while( (line=br.readLine()) != null);
                      }
                      catch(IOException ioe)
                      {              
                          
                      }
                }
            }          
            if(i == 4)
            {
                //A simple instruction that tells the user how to exit the program
                System.out.println("Press the 'x' button in the corner of the window to quit");
            }
            System.out.println("Do you want to exit [y/n]");
            ua = br.readLine();
            }while(ua.equals ("n"));
        }
        catch(IOException ioe)
        {
            
        }
    
    }

}

This is the get functions

public class PropertyToLet
{  
    private String postCode; 
    private String houseNumber;
    private String monthlyRent;
    
    public PropertyToLet (String pc, String hn, String mr)
    {
           postCode = pc;
           houseNumber = hn;
           monthlyRent = mr;
    }
    
    public String getpc()
    {
        return postCode;
    }
    
    public String gethn()
    {
        return houseNumber;   
    }
    
    public String getmr()
    {
        return monthlyRent;   
    }
    
}

This is the reader code

import java.io.*;
import java.util.*;

public class rff
{

    public static PropertyRental proprent = new PropertyRental();
    
   
    public String readFromText()
    {
        String line="";
        String data="";
        
        try
        {
        BufferedReader br = new BufferedReader(new FileReader("properties.txt"));
        line = br.readLine();
        
        

            do// EOF condition
        {
            //System.out.println(line);
            data = data + " \n" + line;
            //line = br.readLine();
            
        }
        while( (line=br.readLine()) != null);
        }
        catch(IOException ioe)
        {
    
        }
        return data;    
    }
}

To "delete" stuff from the text file, you have to read in the entire file, while deciding what you do and don't want in it, then write the output (the stuff you do want in the text file) back to the same file. You can do this one of two ways:

1. While you're reading it in, only add the stuff you do want to a list. Write the contents of that list out to the file.
2. Read everything into a list, then go through the list and delete the elements you don't want. Write out the list.

And it seems like your "delete" code is in (i==2) part of the code, right? You'll need to post an example text file also. We need to know what the format of the text file is in order to help you.

Yeah Sorry, Section 2 is the delete. the text file looks like this

pe158nb,68,500
pe158ul,1,1000


They have been written in as a vector.

any idea on ho0w i would go about doing what you suggested, as i said i'm new to java... and still have trouble piecing bits of code together?

Well, what are the user's options as far as deleting things? How would they specify what lines they want deleted? Does the "pe158nb" distinguish what "kind" of data is on the line? Or does the "nb"? This is information I need to know in order to help you. Basically -- how can the user delete stuff, and how does your program know what stuff to delete (i.e., the program will delete anything that says ub. . etc)?

in another section of the code (i == 3), they are assigned numbers, the user then types in that number on the delete section and it is then deleted from the file... well thats how it should work...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.