I have been given a program that will add and search through records from a random access file.
I need to add an update button so that i can change items in a record and replace the old record with the new one.
The problem is That when i execute the program that the update doesn't happen it just places a new record behind the one i want to update. It also doesn't put in anything but the name.
I have tried putting inputting a string with the name street city state and zip but i'm not sure how to parse it back out.
i know that i need to do something along these lines:
oldvariable = new name/street/city/state/zip(which ever I'm doing at the time)
jbtUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try{
long currentPosition = raf.getFilePointer();
UpdateAddress(currentPosition);
}
catch (IOException ex) {
ex.printStackTrace();
}
}
});
/** Update a record at the specified position **/
public void UpdateAddress(long position){
try{
raf.seek(position);
FixedLengthStringIO.UpdateFixedLengthString(jtfName.getText(), NAME_SIZE, raf);
FixedLengthStringIO.UpdateFixedLengthString(jtfStreet.getText(), STREET_SIZE, raf);
FixedLengthStringIO.UpdateFixedLengthString(jtfCity.getText(), CITY_SIZE, raf);
FixedLengthStringIO.UpdateFixedLengthString(jtfState.getText(), STATE_SIZE, raf);
FixedLengthStringIO.UpdateFixedLengthString(jtfZip.getText(), ZIP_SIZE, raf);
}
catch(IOException ex){
ex.printStackTrace();
}
}
and
public static void UpdateFixedLengthString(String s, int size, DataOutput out) throws IOException{
char[] chars = new char[size];
s.getChars(0, Math.min(s.length(), size), chars, 0);
for (int i = Math.min(s.length(), size); i < chars.length; i++)
chars[i] = ' ';
out.writeChars(new String(chars));
}
}