mercredi 18 mars 2015

How to create Yes / No?

So this code allows the users to save their object, created in the main. However I wish to create a function (if statement i think) which will ask the users whether they wish to save it (results.txt). If they do then save, if not then exit the program. Thanks.



import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Main {

public static void main( String args[] ) //Standard header for main method
{
Person arthur = new Person();
arthur.name = "Arthur Dent";
arthur.age= 21;



System.out.println("\nDo you wish to save these results? (Y / N)");


String fileName = "results.txt";
try {

ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fileName));
os.writeObject(arthur); //write object
os.close();


} catch (IOException e) {
e.printStackTrace();
}

System.out.println("\nYour results are saved!");

}
}

Aucun commentaire:

Enregistrer un commentaire