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 49 of 49

Thread: My program will not run properly!

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program will not run properly!

    it doesnt read the correct values
    Yes that is what debugging is about. Finding out why the program produces incorrect values.
    Then when the incorrect values are found, the program needs to be changed to produce correct values.

    Good luck.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: My program will not run properly!

    You have to be not reading at all like what is going on 0 help

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program will not run properly!

    I'm trying to show you how to debug your code and find the problems in it. By printing out the values and finding an unexpected value (I assume 0 is unexpected) you need to look at the code to see why its initial value was 0. I assume you expected an initial value of 64. Why didn't you ask where the 0 came from?

    The next observation you should make when looking at the printed out values was where did the value of 52 come from? The initial value of 0 minus 12 is -12 not 52.
    So where did 52 come from? I was waiting to point that out, but you never got past saying the output for the second trial was wrong. You need to look at all the values and think about if they are reasonable.

    Your expectations must have been that I would fix the code. I do not fix a student's code. I try to give debugging techniques. All programmers must know how to debug their code.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    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 wasn't expecting you to do my code haha... i was expecting a answer that would help... if you would like i actually revised my code a lot now i just have an issue of a repeating number but ill solve this issue myself considering you don't want to help.

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program will not run properly!

    you don't want to help.
    That is a hard statement. How many responses did I give on this thread? How quickly did I respond?

    What kind of responses were you looking for?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: My program will not run properly!

    Quote Originally Posted by Norm View Post
    That is a hard statement. How many responses did I give on this thread? How quickly did I respond?

    What kind of responses were you looking for?
    Something to help me. I added a while statement but its stuck in a loop and keeps adding the amount of water i put in

     public void addWater(int waterAdd){
        int tempWater;
               do{ tempWater = MAX_JUGOUNCES - waterAdd;
                   System.out.println("waterLeft="+ tempWater + " ounces, waterAddded="+waterAdd + " ounces.");
                  }while(waterAdd <= 12);
      if(waterJug > 64){
          System.out.println("Jug can't exceed 64 ounces!");
     
    }}

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program will not run properly!

    its stuck in a loop
    What condition will stop the looping?
    Does any of the code inside of the loop change the value of any variables used in that condition?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: My program will not run properly!

    System.exit();?
    I dont know of any terms to stop the program from continuing of the condition is not met
    I tried using 'tempWater' but I am completely stuck right now

  9. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program will not run properly!

    Here is the condition that controls the looping:
    (waterAdd <= 12)
    as long as waterAdd is <= 12 the loop will continue.
    Change waterAdd to a value > 12 and the loop will exit.

    What is the purpose of the loop? All it does is compute a value for tempWater and print a message. It does not change anything.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: My program will not run properly!

    So I know what to do but i dont know how.. Once the first equation is done and it prints the message, I want whatever that jug value printed is example("User enters 12... waterJug = 52 waterAdd = 12 Now remeber that 52 value and reapply it to the same equation so that it loops until we hit 0.

  11. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program will not run properly!

    I know what to do
    Please explain in simple steps in a list describing what you want to do.

    For example the steps to get 2 numbers from a user and sum them
    zero out sum field
    ask for first number
    read number
    add number to sum
    ask for next number
    read next number
    add number to sum
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: My program will not run properly!

    Okay no problem.
    1.When method is called user inputs number(stored in 'waterAdd')
    2.This is than followed by a check to see if the value is any larger than 12(otherwise it cant continue)
    3.If value is <= 12, the difference of 'waterAdd' and 'jugOunces'(jugOunces = 64; )
    4. I now have that value stored in 'tempWater' and that is being printed to the users screen.
    5.I now need, 'tempWater' to be called again to instantiate another method call like above but with the new 'tempWater' value instead of 'jugOunces'

  13. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program will not run properly!

    What is 3. supposed to do when value <= 12. I do not see any actions to take.

    In 5. what does 'tempWater' to be called again mean? If tempWater is a variable it can not be called. Its value can be changed.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    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 the method is called the user enters a number
    that number is taken away from jugOunces which equals 64
    is there anything else i can store values in besides variables

  15. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program will not run properly!

    number is taken away from jugOunces which equals 64
    So is the number always subtracted from 64?
    That sounds like what you were complaining about earlier:
    user enters 12 results is 52
    user enters 10 results is 54
    The result is ALWAYS 64 minus what the user entered.

    Don't you want to save that results and subtract the new user input from that saved value, not from 64?
    Some thing like this:
    user enters 12 results is 52
    user enters 10 results is 42 (52 minus 10 NOT 64 minus 10)
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41
    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 exactly what you said is what ive said 3 times. "I dont know how to do it i just know what i wanna do"

  17. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program will not run properly!

    Here is an example of how to subtract or add and remember the new value:
    x  = 10      // set initial value to 10
    x = x - 3   // x is now 7   (10 - 3)
    x = x - 2    // x is now 5    (7 - 2)
    x = x + 4   //  x is now 9   (5 + 4)
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: My program will not run properly!

    This is what i attempted to do for tempWater and you said it wont work

    Wouldnt your example be exactly mine but with x?

  19. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program will not run properly!

    This is what i attempted to do for tempWater
    Please post the complete code that shows what you have attempted.

    Is tempWater the variable that is be decremented by the amounts from the user?

    I did not see any statements in your code that looked like the following:
    tempWater = tempWater - amount; // decrement tempWater by amount
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: My program will not run properly!

    This is what ive added and changed but

    if(waterAdd != 13){
         int tempWater = 64;
     
             tempWater = tempWater - waterAdd; // decrement tempWater by amount
              tempWater = tempWater - waterAdd;
             System.out.println("waterLeft="+ tempWater + " ounces, waterAddded="+waterAdd + " ounces.");
     
            }
    It leads to this

     jug1.addWater(12);
    waterLeft=40 ounces, waterAddded=12 ounces.

  21. #46
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program will not run properly!

    Is that print out what you expected? You didn't add any comments about it.

    Defining tempWater as a local variable (inside of the if statement) means that its value is LOST as soon as execution leaves the if statement.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    jcruz16 (October 31st, 2019)

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

    Default Re: My program will not run properly!

    Oh ok so is there anything else i can use? so that the value is remembered for next time

  24. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: My program will not run properly!

    so that the value is remembered for next time
    Define the variable at the class level, not local.
    public class TheClass {
        // here is class level
       int varname;   // define a variable at the class level
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    jcruz16 (October 31st, 2019)

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

    Default Re: My program will not run properly!

    Thank you so much that solved everything!!!!

Page 2 of 2 FirstFirst 12

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