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 1 of 2 12 LastLast
Results 1 to 25 of 34

Thread: Code won't let me remove integer.

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

    Default Code won't let me remove integer.

    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.
    Last edited by DarkestLord; May 7th, 2014 at 10:48 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    it won't let me subtract copper.
    Can you post the output from the program that shows what you are talking about?

    Add some println() statements that print out the values of the variables as they are used so that they can be copied from the console to post here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    i think you should compare silver/10 and copper/100 with total value when you remove it.

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

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

    Quote Originally Posted by huangliyuan View Post
    i think you should compare silver/10 and copper/100 with total value when you remove it.
    What do you mean? Sorry, I'm new. ^^;

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    completely ignoring my silver and copper subtraction.
    Where does the program show a message like this:?
    "Your purse now has xxxx copper."

    Or for silver?
    If you don't understand my answer, don't ignore it, ask a question.

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

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

    Quote Originally Posted by Norm View Post
    Where does the program show a message like this:?
    "Your purse now has xxxx copper."

    Or for silver?
    Nowhere yet. Right now, they are supposed to convert into GP. Gold is 1 GP, silver is 1/10 of a GP, copper is 1/100 of a GP, and platinum is 10 GP. When I add them, everything works out fine and they convert like they're supposed to. When I subtract them though, it ignores the silver and copper.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    When I subtract them though, it ignores the silver and copper.
    Can you post the program's output that shows what you are talking about?

    I don't see where there are any println statements for silver or copper. Please post the current version of the code that has println statements for silver and copper and add show strong comments like:
    //>>>>>>>>>>***** HERE is the println for silver <<<<<<<<<<<*****
    to make it easy to find.

    Print out the values of:
    public static final double SILVER_VALUE = 1/10;
    public static final double COPPER_VALUE = 1/100;
    If you don't understand my answer, don't ignore it, ask a question.

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

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

    OK I editted my post. The code now has the println and at the bottom, I show the values I get. I set them to 1/10 and 1/100 respectively, but I only get 0 for some reason...

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    1/10 and 1/100
    I only get 0 for some reason...
    The reason is integer division: 2/10 = 0
    vs float division: 2/10.0 = 0.2
    If you don't understand my answer, don't ignore it, ask a question.

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

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

    Quote Originally Posted by Norm View Post
    The reason is integer division: 2/10 = 0
    vs float division: 2/10.0 = 0.2
    Ahhh...well now the values are coming up correctly as

    1.0
    0.1
    0.01
    10.0

    I changed the values to 1/10.0 and 1/100.0 respectively.
    But I'm still dealing with the issue in which I can add currency but not subtract silver or copper.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    What value is not being computed correctly?
    Try debugging the code by adding some println() statements to print out the value of all the variables used in the computations so you can see what the computer is doing.
    If you don't understand my answer, don't ignore it, ask a question.

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

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

    Well like I said, I changed the values and added .0 at the end. Now they show up as .1 and .01 on printlin() like they should. But when I try to subtract silver or copper from the total GP, nothing happens.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    nothing happens.
    What value is not being computed correctly?
    Try debugging the code by adding some println() statements to print out the value of all the variables used in the computations so you can see what the computer is doing.
    If you don't understand my answer, don't ignore it, ask a question.

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

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

    Quote Originally Posted by Norm View Post
    What value is not being computed correctly?
    Silver and Copper. When I add them (100 gold+200 silver and 4000 copper=160 gold) it works fine. But when I subtract them (100 gold-200 silver and 4000=100 gold) it acts like nothing happened. I already put in println() statements on the code. It gives me the values I should (1.0 for gold, .1 for silver, .01 for copper, and 10.0 for platinum), but when I try to subtract silver and copper, it doesn't work.

    I don't see anywhere else I could add a println() statement and learn from it because I add and subtract the variables from the Pursetest Class, not the Purse Class.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    I don't see anywhere else I could add a println() statement and learn from it
    Then you need to learn how to debug.
    Print out the value of gp and silver at the start and end of the removeSilver() method.
    Print out the value returned by removeSilver().
    If you don't understand my answer, don't ignore it, ask a question.

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

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

    Quote Originally Posted by Norm View Post
    Then you need to learn how to debug.
    Print out the value of gp and silver at the start and end of the removeSilver() method.
    Print out the value returned by removeSilver().
    Compiler won't let me put

    System.out.println(SILVER_VALUE);
    System.out.println(silver);

    at the end of the statement. It says they are both unreachable for some reason. It allows me to put it at the begining of the removeSilver() method, but I have no idea how I could access it to see.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    Can you post the code with the errors?
    The println statements should be the first thing inside the method and at the end just before the return statement.
    After the return is unreachable.

    Print both gp and silver
    If you don't understand my answer, don't ignore it, ask a question.

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

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

    Edited the post. Now I can add it at the end, but I have no clue how I can access it. It only lets me visit the main method.

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    What is printed out when the program is executed?

    There is no need to print SILVER_VALUE.
    If you don't understand my answer, don't ignore it, ask a question.

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

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

    Quote Originally Posted by Norm View Post
    What is printed out when the program is executed?
    I tried doing the Pursetest again. I added 100 gold and nothing else.

    "Your purse now has 100.0 gold."

    Then I only subtracted Silver. I subtracted 300 pieces and got this

    "300.0
    100.0
    300.0
    100.0
    Your purse now has 100.0 gold."

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    What was the value returned by the removeSilver() method?

    What are the names of the variables for the values that were printed? The println() statements need to label the print out so you can tell which is which:
    System.out.println("an ID "+ theVariableHere); // change "an ID" to a unique name
    If you don't understand my answer, don't ignore it, ask a question.

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

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

    Quote Originally Posted by Norm View Post
    What was the value returned by the removeSilver() method?
    300.0
    100.0
    300.0
    100.0

    The 300.0 is the silver I tried to subtract and the 100.0 is the GP that was already there.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    What does the if statement do with those values?

    --- Update ---

    You forgot to answer this question:

    What was the value returned by the removeSilver() method?
    If you don't understand my answer, don't ignore it, ask a question.

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

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

    Quote Originally Posted by Norm View Post
    You forgot to answer this question:

    What was the value returned by the removeSilver() method?
    That's what this is

    300.0
    100.0
    300.0
    100.0

    These are the only values I get from removeSilver();

    300.0 is the silver I tried to remove and 100.0 is the GP I had before and after.

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

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

    The removeSilver() method returns a boolean:
    public boolean removeSilver(double silver)
    What is the value of that boolean? What does it mean if true and if false?

    300.0 is the silver I tried to remove and 100.0 is the GP I had before
    What does the if statement in removeSilver() do with those values?
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

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