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 49

Thread: My program will not run properly!

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default My program will not run properly!

    Write a method that lets a caller add water taken from a 64-ounce jug into an empty 12-ounce bottle. Assume there are already fields for jugOunces and bottleOunces. The number of ounces to be poured into the bottle is determined when the method is called(using a parameter). If too much water is added it will spill and be lost so the method should prevent the caller from adding too much. We also need to keep track of how much water is still in the jug so we don't add whats no longer there.

    public class Jug {
        private static int jugOunces = 64;
        private static int bottleOunces;
        public static int waterJug;
     
        public Jug() {
            jugOunces = 64;
            bottleOunces = 12;
        }
     
    public static void main(int waterAdd){
        //ERROR HERE 
     
            if (waterAdd > 13) {
                System.out.println("Bottle can only hold 12 ounces!");
                waterJug = 64;
            }
            else if(waterAdd <= 12)
                waterJug = jugOunces - waterAdd;
            printInfo();
     
            }
        public static void printInfo() {
            System.out.println("There is " + waterJug + "ounces left in the jug.");
        }
    }




    It must never end and allow user to keep pouring water until jug of 64 ounces is empty

  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: My program will not run properly!

    Can you copy the contents of the command prompt window that shows the program's input and output and paste it here to show what you are talking about?
    Add some comments to the post where the output is not what you want it to be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My program will not run properly!

    I am using BlueJ as its what we use in class. Maybe this?

    Jug jug2 = new Jug();
    Jug.main(12);
    There is 52ounces left in the jug.
    Jug.main(14);
    Bottle can only hold 12 ounces!
    There is 64ounces left in the jug.
    Jug.main(12);
    There is 52ounces left in the jug.

  4. #4
    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: My program will not run properly!

    Ok, where does that print out show any problems?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2019
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My program will not run properly!

    My issue is if I call that method again, it should keep reducing the ounces in the jug. I do not know how to tell java "hey remember this value for next method call".

  6. #6
    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: My program will not run properly!

    Can you post the current output and add some comments to that output where the value is wrong?
    For example
    test 1
    other 2 <<<<<<< HERE 2 should be 3

    Note: java programming conventions define the main method this way:
     public static void main(String[] args) {
    The main method in the program you posted should have a name referring to what it does. For example: addWater
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2019
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My program will not run properly!

    public static void addWater(int waterAdd){
     
     
            if (waterAdd > 13) {
                System.out.println("Bottle can only hold 12 ounces!");
                waterJug = 64;
            }
            else if(waterAdd <= 12)
                waterJug = jugOunces - waterAdd;
            printInfo();
     
            }

    If I input 12 it comes out to 52, fine! but if i call the same method again and input 12 it doesnt go to 40... but instead 52 comes out i need THIS FIXED.

  8. #8
    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: My program will not run properly!

    Ok, time for some debugging. One way is to add print statements in the code that print out the values of all the variables as they are used and as their values are changed. If you know how the values should be changed by the program, when you see what the actual values are, you should be able to find the problem.

    Recommended format:
       System.out.println("var1="+var1 + ", var2="+var2);
    Change var1 and var2 to variables that are in the program and add any other variables you want to see.


    it doesnt go to 40... but instead 52
    Where is the previous value saved?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2019
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My program will not run properly!

    I already have a print method i am just calling it after the method is complete
    I only need help with the values. I am storing it in waterJug...
    When the method is called the user inputs a number to "fill" the bottle.
    The number cant be more than 12 because the bottle only holds 12 ounces but we need to empty 64 ounces out of a bigger jug....

  10. #10
    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: My program will not run properly!

    need help with the values.
    Add print statements as I suggested so you can see how the contents of the variables change as the program executes.

    Did you see this question I asked?
    Where is the previous value saved?

    What variable holds the current value? When is it changed? What is the new value after the change?
    Add some print statements that show the answers those questions.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2019
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My program will not run properly!

    public static void printInfo() {
            System.out.println("There is " + waterJug + "ounces left in the jug.");
        }
    }

    I already have the print value
    Yes i read your question and i answered it
    I am storing it in waterJug...
    When the method is called the user inputs a number to "fill" the bottle.
    The number cant be more than 12 because the bottle only holds 12 ounces but we need to empty 64 ounces out of a bigger jug....

  12. #12
    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: My program will not run properly!

    I already have the print value
    But you need to see its value at other places in the program.

    Add this print statement first thing in the addWater method:
           System.out.println("waterJug="+waterJug + ", waterAdd="+waterAdd);
    Look at what is printed. Do the printed values make sense?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2019
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My program will not run properly!

    Ok i added
    System.out.println("waterJug="+waterJug + ", waterAdd="+waterAdd);

    and the values make sense but how do i now make this continuous? I need to be able to call this method again and it return "40"

  14. #14
    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: My program will not run properly!

    the values make sense
    Please copy the output and paste it here so I can see what is printed.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Oct 2019
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My program will not run properly!

    waterJug = 52 waterAdd= 12

    if i call again it should be

    waterJug= 40 waterAdd= 24

  16. #16
    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: My program will not run properly!

    Where is the rest of the print out from the program?
    There should be print outs like this in what was printed:
    There is 52ounces left in the jug.
    Bottle can only hold 12 ounces!
    There is 64ounces left in the jug.
    Please copy the contents of the console and paste it here. Do not type in what you thought you saw.

    Also post the new version of the code.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Oct 2019
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My program will not run properly!

    Its not what i thought i say cause i tried it
     waterJug=52, waterAdd=12
    same thing

    public class Jug {
        private static int jugOunces = 64;
        private static int bottleOunces;
        public static int waterJug;
     
        public Jug() {
            jugOunces = 64;
            bottleOunces = 12;
        }
     
    public static void main(int waterAdd){
        //ERROR HERE 
     
            if (waterAdd > 13) {
                System.out.println("Bottle can only hold 12 ounces!");
                waterJug = 64;
            }
            else if(waterAdd <= 12)
                waterJug = jugOunces - waterAdd;
      System.out.println("waterJug="+waterJug + ", waterAdd="+waterAdd);
     
            }
     
     
     
     
     
     
     
        public static void printInfo() {
            System.out.println("There is " + waterJug + "ounces left in the jug.");
        }
    }

  18. #18
    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: My program will not run properly!

    The print statement you added for debugging is supposed to be the first statement in the main method.
    The idea is to see the values before they are changed and again after they are changed.

    What happened to the name change I suggested?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Oct 2019
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My program will not run properly!

    When i apply it first i get
    Jug.main(12);
    waterJug=0, waterAdd=12

  20. #20
    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: My program will not run properly!

    Does that look right? Is 0 a good value?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Oct 2019
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My program will not run properly!

    No I already stated it should just reduce it to 52 if i enter 12 and I need it to be continuous so i can call it again and it reduces more...

  22. #22
    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: My program will not run properly!

    Is it a problem that it has a value of 0 the first time the method is executed?
    What should its first value be?

    Please be sure to always copy and paste here the full contents of the console. Do not copy just one line.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Junior Member
    Join Date
    Oct 2019
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My program will not run properly!

    Yes it is a problem.....It should start at 64. Once the method is called the user inputs a number(to fill empty 12 OUNCE BOTTLE) from the 64 OUNCE CONTAINER. If i use your print method after like this
     public class Jug {
        private static int jugOunces = 64;
        private static int bottleOunces;
        public static int waterJug;
     
        public Jug() {
            jugOunces = 64;
            bottleOunces = 12;
        }
     
    public static void main(int waterAdd){
        //ERROR HERE 
     
            if (waterAdd > 13) {
                System.out.println("Bottle can only hold 12 ounces!");
                waterJug = 64;
            }
            else if(waterAdd <= 12)
                waterJug = jugOunces - waterAdd;
      System.out.println("waterJug="+waterJug + ", waterAdd="+waterAdd);
     
            }
    the values are correct and i get
      waterJug=52, waterAdd=12
    THE ISSUE IS I need to be able to call this method again to further lower the 64 OUNCE CONTAINER until it is empty

  24. #24
    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: My program will not run properly!

    How did you fix the initial value of 0?

    Again the posted code does NOT have the debug print statement in the correct location. It needs to be first thing in the main method. You need to see the values at the starting point of the method.


    This is starting to get old. I make suggestions for debugging and you ignore them.

    Debugging means finding and fixing problems.
    0 was a problem. That needs to be fixed before going to the next problem.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Junior Member
    Join Date
    Oct 2019
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: My program will not run properly!

    If i put it first thing in the method it doesnt read the correct values as i stated 3 times
    I will just hand this in like this thank you .......
    Again the print method is not the issue please just tell me how to make this method continuous please thats it my original code does the job just needs to be continuous please thats it
    Last edited by jcruz16; October 30th, 2019 at 06:48 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Need help on making a mastermind game program work properly
    By aude922000 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2013, 07:42 PM
  2. [SOLVED] Is this coded properly?
    By The_pizzaguy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 16th, 2013, 01:28 AM
  3. Cannot get the program to write and display properly
    By jimbo62 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 2nd, 2013, 08:58 PM
  4. Temperature Converter Program is not running properly
    By jessieaugust in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 5th, 2013, 11:31 AM
  5. Validation based program that doesn't function properly
    By kratos75 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2011, 06:34 AM

Tags for this Thread