I have program i am trying to do. I was successful in checking to see if the file was created and if it was not created then creating it. Now I want to actually put data it the file and i am getting an exception error. Writing to a file should be very easy. Not sure where I am going wrong with this.
import java.io.File; import java.io.*; import java.util.Formatter; import java.util.Scanner; /** * * @author Jamal */ public class Player { private int wonAGame; private int lostAGame; private int addMoney; private static String fPlayer; private Formatter x; Scanner sn = new Scanner(System.in); public String getPlayerinfo() { return fPlayer; } public void playerInfo(String fName) { System.out.println("Please enter your first name: "); String fname = sn.nextLine(); fPlayer = fname; Player pr = new Player(); pr.checkPlayer(fname); } public void checkPlayer(String fname) { File file = new File("C:\\test\\" + fPlayer + ".txt"); if (file.exists()) { System.out.println("Weclome back" + " " + fPlayer); } else { System.out.println("Sorry not there"); try { Formatter x = new Formatter("C:\\test\\" + fPlayer + ".txt"); System.out.println("File has been created"); } catch (Exception e) { System.out.println("didn't work"); } Player pr = new Player(); pr.writeFile(); } } public void writeFile() { x.format("%s,%s", "LastName, First Name"); } public void closeFile() { x.close(); } }