Question: Can someone please fix my edit retrieveFromFile and saveToFile method so that they fit this criteria. The way they currently are they have 0 functionality.

Can someone please fix my edit retrieveFromFile and saveToFile method so that they fit this criteria. The way they currently are they have 0 functionality. It should not be a printout to console.
Itemized Part 3 requirements of user interaction
1. The user should be able to save a current game to file, to
finish it later by retrieving it from file. As such, the methods saveToFile and retrieveFromFile should be added
to the game class. The file extension for the game file
should be .gam or .tet .
2. When saving a game to a file, the user must be able to enter
a unique file name. The program should add the extension.
3. You must be able to retrieve a game back from a .gam
or .tet file using a GUI Interface to select file (JFileChooser)
public void saveToFile()
{
String[] options={"Ok","Cancel"};
String title=JOptionPane.showInputDialog("Put your name ");
JFileChooser fileChooser = new JFileChooser(FileSystemView.getFileSystemView());
fileChooser.showSaveDialog(fileChooser);
try (PrintWriter writer = new PrintWriter(new FileWriter(title +".tar"))){
// Write game state to file
writer.println(rows);
writer.println(cols);
writer.println(numBrickTypes);
writer.println(state);
writer.println(score);
// Write background array to file
for (int i =0; i rows; i++){
for (int j =0; j cols; j++){
writer.print(background[i][j]+"");
}
writer.println();
}
// Write fallingBrick details to file
writer.println(getRows());
writer.println(getCols());
// writer.println(fetchBoardPosition(int row,));
writer.println(fallingBrick.getColorNumber());
writer.println(Arrays.toString(fallingBrick.getPosition()));
writer.println(fallingBrick.getOrientation());
writer.println(getScore());
writer.close();
} catch (IOException e){
e.printStackTrace();
}
}
public void retrieveFromFile()
{
String fName=null;
File fileConnection = new File(fName);
try
{
Scanner inScann = new Scanner(fileConnection);
rows = inScann.nextInt();
cols = inScann.nextInt();
background = new int[rows][cols];
for(int row =0; row rows;row++)
{
for(int col =0; col cols;col++)
{
background[row][col]= inScann.nextInt();
}
}
}catch(Exception e)
{
System.err.print("Error occurred during retrieve from file ");
}
}
Can someone please fix my edit retrieveFromFile

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!