Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Writing a unique field to a RAF file help

    Hello, so I am trying to write a unique ID number to each new record I add to my random access file, but I'm having some trouble. I can't figure out a way to do this so it increases each time, instead it just has the same number written for every record. Can someone explain to me how to have the ID number increase by 1 each time I write? I'm appending to the end of the file each time if that helps.

    Thank you for any help.

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Can you please post your existing code so we can help you correct it?
    Software and cathedrals are much the same - first we build them, then we pray.

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    public void write(boolean append){

    LinkedList Ln = new LinkedList();

    String book_name = bookname_tb.getText();
    String delete = delete_tb.getText();

    String file_path = "123abc123abc";

    Library_database a_data = new Library_database(book_name, delete);
    Ln.add(a_data);

    char[] char_bookName = new char[150];
    char char_delete = 0;

    //book name
    for(int a = 0; a < char_bookName.length; a++){//as long as a is less than the length of char_bookName keep running
    char_bookName [a] = ' ';//fills the array
    }

    //overrides the spaces with the book's name
    for(int i = 0; i < book_name.length(); i++){//as long as i is less than the length of book_name keep running
    if(i >= 149) break;//stops overflow

    char_bookName[i] = book_name.charAt(i);//finds the character position in the array, so you can print individual letters
    }


    for(int i = 0; i < 1; i++){
    char_delete = delete.charAt(i);
    }


    //writing stuff
    try //try and catch statement for stopping errors crashing the program
    { //for writing to a file
    DataOutputStream out = new DataOutputStream(//creates an object
    new FileOutputStream(file_path,true));

    //book name
    for(int k = 0; k < char_bookName.length; k++){//as long as k is less than the length of char_bookName keep running
    char writing_book_name = char_bookName[k];//passes the values into the indexes
    out.writeChar(writing_book_name);//passes the converted values of the variable to out
    }

    for(int k = 0; k < 1; k++){
    char writing_delete = char_delete;
    out.writeChar(writing_delete);
    }

    //indexing
    for(int j = 0; j < Ln.size(); j++){
    int writing_id = Ln.size() +1;
    out.writeInt(writing_id);
    System.out.println(writing_id);
    }

    System.out.println("WRITE FUNCTION IS WORKING");//testing, will delete on final version

    out.close(); //tells it to stop writing
    }

    //catches errors and displays a message
    catch (IOException iox)//catches an error with the input/output
    {
    System.out.println("Problem writing " + file_path);//tells you if there's an error, catch to the try statement
    }
    }


    Does this help at all? :S


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •