EDIT: New issue. My program gives me an error when I try to "digUp" the purse. The error appears in the purse class.
import java.io.*; public class Purse { //Fields public double gp; public double gold; public double silver; public double copper; public double platinum; public static final double GOLD_VALUE = 1; public static final double SILVER_VALUE = 1/10.0; public static final double COPPER_VALUE = 1/100.0; public static final double PLATINUM_VALUE = 10/1; //Constructors public static void main(String[] args) { //>>>>>>>>>>***** HERE is the println for the values <<<<<<<<<<<***** System.out.println(GOLD_VALUE); System.out.println(SILVER_VALUE); System.out.println(COPPER_VALUE); System.out.println(PLATINUM_VALUE); //>>>>>>>>>>***** HERE ends the println for the values <<<<<<<<<<<***** } public Purse() { //initialize gp=0; gold=0; silver=0; copper=0; platinum=0; } //Methods public void addGold(double gold) { gp+=gold*GOLD_VALUE; } public void addSilver(double silver) { gp+=silver*SILVER_VALUE; } public void addCopper(double copper) { gp+=copper*COPPER_VALUE; } public void addPlat(double platinum) { gp+=platinum*PLATINUM_VALUE; } public boolean removeGold(double gold) { boolean enoughmoney=true; if(gold>gp) enoughmoney=false; else gp-=gold*GOLD_VALUE; return enoughmoney; } public boolean removeSilver(double silver) { boolean enoughmoney=true; if(silver>gp) enoughmoney=false; else gp-=silver*SILVER_VALUE; return enoughmoney; } public boolean removeCopper(double copper) { System.out.println(copper); System.out.println(gp); boolean enoughmoney=true; if(copper>gp) enoughmoney=false; else gp-=copper*COPPER_VALUE; System.out.println(copper); System.out.println(gp); return enoughmoney; } public boolean removePlat(double platinum) { boolean enoughmoney=true; if(platinum>gp) enoughmoney=false; else gp-=platinum*PLATINUM_VALUE; return enoughmoney; } public double getValue() { double gp=(GOLD_VALUE*gold)+(SILVER_VALUE*silver)+(COPPER_VALUE*copper)+(PLATINUM_VALUE*platinum); return gp; } public void printReport() { System.out.println("The purse now contains:"); System.out.println("\t" + platinum + " platinum pieces"); System.out.println("\t" + gold + " gold pieces"); System.out.println("\t" + silver + " silver pieces"); System.out.println("\t" + copper + " copper pieces"); System.out.println("\tAnd has a "+ this.getValue() + " GP value."); } public void buryPurse() { stringToFile(Double.toString(gp),"gold.txt"); stringToFile(Double.toString(gp),"silver.txt"); stringToFile(Double.toString(gp),"copper.txt"); stringToFile(Double.toString(gp),"platinum.txt"); } public void digUpPurse() { //***THE PROBLEM IS HERE*** gp=Integer.getInteger(file2String("gold.txt")); gp=Integer.getInteger(file2String("silver.txt")); gp=Integer.getInteger(file2String("copper.txt")); gp=Integer.getInteger(file2String("platinum.txt")); //***THE PROBLEM IS HERE*** } private void stringToFile( String content, String fileName ) { try { File file = new File( fileName ); // if file doesnt exists, then create it if ( ! file.exists( ) ) { file.createNewFile( ); } FileWriter fw = new FileWriter( file.getAbsoluteFile( ) ); BufferedWriter bw = new BufferedWriter( fw ); bw.write( content ); bw.close( ); //System.out.println("Done writing to " + fileName); //For testing } catch( IOException e ) { System.out.println("Error: " + e); e.printStackTrace( ); } } //End method stringToFile public String file2String(String filename) { try { FileReader fileReader = new FileReader(filename); String fileContents = ""; int i ; while((i = fileReader.read())!=-1) { char ch = (char)i; fileContents = fileContents + ch; }//end while //System.out.println(fileContents); return fileContents; }catch(Exception e) { System.out.println("Error: " + e); return "Error " + e; }//end catch }//end file2String }
And here's my test for it.
import javax.swing.JOptionPane; public class PurseTest { public static void main(String[] args) { String plusGold = JOptionPane.showInputDialog("Enter Gold to add"); String plusSilver = JOptionPane.showInputDialog("Enter Silver to add"); String plusCopper = JOptionPane.showInputDialog("Enter Copper to add"); String plusPlatinum = JOptionPane.showInputDialog("Enter Platinum to add"); Purse myPurse=new Purse(); double gold = Integer.parseInt(plusGold); double silver = Integer.parseInt(plusSilver); double copper = Integer.parseInt(plusCopper); double platinum = Integer.parseInt(plusPlatinum); myPurse.addGold(gold); myPurse.addSilver(silver); myPurse.addCopper(copper); myPurse.addPlat(platinum); System.out.println(" "); System.out.println("The purse now contains:"); System.out.println("\t" + platinum + " platinum pieces"); System.out.println("\t" + gold + " gold pieces"); System.out.println("\t" + silver + " silver pieces"); System.out.println("\t" + copper + " copper pieces"); System.out.println("\tAnd has a "+ myPurse.gp + " GP value."); String takeGold = JOptionPane.showInputDialog("Enter Gold to subtract"); String takeSilver = JOptionPane.showInputDialog("Enter Silver to subtract"); String takeCopper = JOptionPane.showInputDialog("Enter Copper to subtract"); String takePlatinum = JOptionPane.showInputDialog("Enter Platinum to subtract"); gold = Integer.parseInt(takeGold); silver = Integer.parseInt(takeSilver); copper = Integer.parseInt(takeCopper); platinum = Integer.parseInt(takePlatinum); myPurse.removeGold(gold); myPurse.removeSilver(silver); myPurse.removeCopper(copper); myPurse.removePlat(platinum); System.out.println(" "); System.out.println("The purse now contains:"); System.out.println("\t" + platinum + " platinum pieces"); System.out.println("\t" + gold + " gold pieces"); System.out.println("\t" + silver + " silver pieces"); System.out.println("\t" + copper + " copper pieces"); System.out.println("\tAnd has a "+ myPurse.gp + " GP value."); System.out.println(" "); System.out.println("The purse will now be buried."); myPurse.buryPurse(); System.out.println("The purse now contains:"); System.out.println("\t" + platinum + " platinum pieces"); System.out.println("\t" + gold + " gold pieces"); System.out.println("\t" + silver + " silver pieces"); System.out.println("\t" + copper + " copper pieces"); System.out.println("\tAnd has a "+ myPurse.gp + " GP value."); System.out.println("The purse will now be unburied."); myPurse.digUpPurse(); System.out.println("The purse now contains:"); System.out.println("\t" + platinum + " platinum pieces"); System.out.println("\t" + gold + " gold pieces"); System.out.println("\t" + silver + " silver pieces"); System.out.println("\t" + copper + " copper pieces"); System.out.println("\tAnd has a "+ myPurse.gp + " GP value."); }//End main }//end Purse Test
In the digUpPurse() method, it tells me
java.lang.NullPointerException
null
I have no idea what this means.