Can someone please show me how to get this code working correctly. It doesn\'t h
ID: 3543856 • Letter: C
Question
Can someone please show me how to get this code working correctly. It doesn't have to be exact and I know you don't know the exact methodNames or variables, just show how to do the steps listed in this code with accurate java coding.
public String update(String category, Question q, int num) {
// Get the vector of questions from a category file
// Check that the index number is valid (between 0 and the vector size - 1)
// If valid, replace the question at that index in the vector with the new
// question object, then save the file and return the result of the
// save operation
// If index is invalid return a string indicating that.
}
Explanation / Answer
import java.io.*;
import java.util.Vector;
public class VectorFile {
private String saveFile(String category, Vector<Question> questions) {
// Create the file object
// category is a file name inside the base directory
File F = new File(category);
// Inside a try block,
try{
// Create FileOutputStream for the category file
FileOutputStream saveFile = new FileOutputStream(F, true);
// Create ObjectOutputStream for the category file
ObjectOutputStream save = new ObjectOutputStream(saveFile);
// Write the vector to the file
save.writeObject(questions);
// Flush the object stream, then close both streams
save.flush();
save.close();
saveFile.close();
// Return a string indicating success
return "Success";
}
// Catch blocks:
catch(Exception E){
E.printStackTrace();
// Return an error string indicating the problem
return "Error in Writing to the File " + category;
}
}
private Vector<Question> getCategoryFile(String category) {
//Create the file object:
// category is a file name inside the base directory
File F = new File(category);
// Create an empty vector of questions
Vector<Question> questionVector;
// Inside a try block,
try{
// Create FileInputStream for the category file
FileInputStream saveFile = new FileInputStream(F);
// Create ObjectInputStream for the category file
ObjectInputStream save = new ObjectInputStream(saveFile);
// Read the vector from the file
questionVector = (Vector<Question>) save.readObject();
// Close both streams
save.close();
saveFile.close();
}
// Catch blocks:
catch(Exception E){
E.printStackTrace();
//Create an empty vector of questions
questionVector = new Vector<Question>();
}
// Return the vector
return questionVector;
}
public String update(String category, Question q, int num) {
// Get the vector of questions from a category file
Vector<Question> questions = getCategoryFile(category);
//String variable to hold the result of save operation
String result;
// Check that the index number is valid (between 0 and the vector size - 1)
if(num>=0 && num<questions.size()){
// If valid, replace the question at that index in the vector
// with the new question object
questions.set(num, q);
//then save the file and return the result of the save operation
result = saveFile(category,questions);
return result;
}
else{
// If index is invalid return a string indicating that.
return "Invalid Index!!";
}
}
}