Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-24-2012, 04:01 AM   PM User | #1
dabananaman
New Coder

 
Join Date: Dec 2010
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
dabananaman is an unknown quantity at this point
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.
dabananaman is offline   Reply With Quote
Old 02-24-2012, 02:48 PM   PM User | #2
ckeyrouz
Senior Coder

 
ckeyrouz's Avatar
 
Join Date: Jun 2009
Location: Montreal, Canada
Posts: 1,044
Thanks: 5
Thanked 179 Times in 179 Posts
ckeyrouz is on a distinguished road
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.
ckeyrouz is offline   Reply With Quote
Old 02-25-2012, 04:09 AM   PM User | #3
dabananaman
New Coder

 
Join Date: Dec 2010
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
dabananaman is an unknown quantity at this point
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
dabananaman is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:39 PM.


Advertisement
Log in to turn off these ads.