Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 34 of 34

Thread: Code won't let me remove integer.

  1. #26
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code won't let me remove integer.

    After I get this working, I plan to keep track of individual amounts of currency. For example, you won't be able to remove 400 silver if you only have 10 silver. But this is something I haven't really implemented yet.

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code won't let me remove integer.

    Ok.

    How is the debugging going? Have you found the problem yet?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code won't let me remove integer.

    I fixed the silver/copper problem, but now I have a new issue. I editted the code and original post.

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code won't let me remove integer.

    now I have a new issue.
    Please copy the full text of the error message and paste it here.

    Continually editing the first post destroys the chain of events for the problems and makes it confusing and hard to follow.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code won't let me remove integer.

    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)
    {
    System.out.println(GOLD_VALUE);
    System.out.println(SILVER_VALUE);
    System.out.println(COPPER_VALUE);
    System.out.println(PLATINUM_VALUE);
    }
    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.

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code won't let me remove integer.

    You need to copy and paste here the full text of the stacktrace printed by the error. It has important information about the error.

    --- Update ---

    BTW The code has lost its formatting. Nested statements (within {}s) need to be indented. Unformated code is hard to read.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #32
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Code won't let me remove integer.

    "java.lang.NullPointerException
    null"

    Is all it gave me. Also, what's a stacktrace?

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Code won't let me remove integer.

    what's a stacktrace?
    The output from the printStackTrace() method. That method needs to be called in all the catch blocks. Just printing a message is not enough.

    The error message you posted is not much use in finding the error. You need to get a stack trace
    If you don't understand my answer, don't ignore it, ask a question.

  9. #34
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Code won't let me remove integer.

    Exception in thread "main" java.lang.NullPointerException
    at pathfinder.Purse.digupPurse(Purse.java:101)
    at pathfinder.PathfinderTester.main(PathfinderTester. java:57)


    which leads to lines:
     gp = Integer.getInteger(file2String("gold.text"));
    and
     myPurse.digUpPurse();

    From my best guess: gp should be gold = Integer.getInteger(file2String("gold.text"));
    with the rest of the method changed accordingly.

    but I'd further guess that the inherit problem seems to be coming from the file2String method or from trying to gather an Integer for a Double variable.

    file2String and stringToFile are both provided by a third part. This is a college Lab Assignment. And before you ask, No. This is not one of my assignments. A friend of mine is taking this class and I'm trying to offer help to him. 99% sure the OP is not my friend.

    ~edit~
    After working with what I had, and what the OP had, I discovered many more inconsistencies in the OP's code. The least of which relating to the presence of a printReport method (which is also provided). I'll work through this on my own time. I'm a beginner to Java myself and I took up the challenge myself to better test myself and improve.
    Last edited by Bishamonten85; May 19th, 2014 at 03:32 AM. Reason: Didn't want to double post.

  10. The Following User Says Thank You to Bishamonten85 For This Useful Post:

    GregBrannon (May 19th, 2014)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. [SOLVED] What will the compiler remove from the code?
    By Cornix in forum Java IDEs
    Replies: 4
    Last Post: December 8th, 2013, 05:57 AM
  2. Why won't this code loop?
    By Joniverse in forum Loops & Control Statements
    Replies: 5
    Last Post: March 29th, 2012, 10:14 AM
  3. Code won't compile
    By JavaChallenged in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 12th, 2012, 08:18 PM
  4. Help with code for converting 4 digit string to integer
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 09:38 PM
  5. [SOLVED] Please Review My Code (Long Integer Addition)
    By Saradus in forum Java Theory & Questions
    Replies: 10
    Last Post: July 4th, 2009, 12:55 PM