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.

Results 1 to 10 of 10

Thread: Can Define what is this error

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Can Define what is this error

    its been a while since i left this program without clarification...
    the code is a bit large.. so i tried to reconstruct the part where the error occurs


    this is the Class
    public class CoffeeBeans {
     
        private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     
        private int numberOfOrders;
        private boolean orderAgain;
     
        public CoffeeBeans() {
     
            numberOfOrders = 0;
        }
     
        public void setOrder(int numberOfOrders) throws IOException {
     
            this.numberOfOrders = numberOfOrders;
     
            if ((this.numberOfOrders < 20) && (this.numberOfOrders >= 5)) {
     
                do {
     
                    System.out.print("\n");
                    System.out.println("Too Small");
                    System.out.println(numberOfOrders);  // why is the value of this PARAMETER stuck in the first value that had been passed to it? (why does it never change?)
                    orderAgain = reOrder();
                }
                while((orderAgain == true) && (numberOfOrders < 20)); // IM HAVING PROBLEM WITH THIS PART
            }
        }
     
        public void showOrder() {
     
            System.out.println("Number of orders: " + numberOfOrders);
        }
     
        private boolean reOrder() throws IOException {
     
            System.out.print("\n");
            System.out.print("Do You Wish To Order Again? ");
            String response = br.readLine();
     
            if ((response.equalsIgnoreCase("Y")) || (response.equalsIgnoreCase("Yes"))) {
     
                numberOfOrders = 0;
     
                System.out.print("\n");
                System.out.print("Enter Your New Order ");
                numberOfOrders = Integer.parseInt(br.readLine());
     
                setOrder(numberOfOrders);
                return true;
            }
            else if ((response.equalsIgnoreCase("N")) || (response.equalsIgnoreCase("No"))) {
     
                return false;
            }
            else {
     
                return false;
            }
     
        }
    }


    this is the Main Class
    public class MainClass {
     
        private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     
        public static void main(String[] args) throws IOException {
     
            CoffeeBeans cb = new CoffeeBeans();
     
            System.out.print("Enter Your Order: ");
            int order = Integer.parseInt(br.readLine());
     
            cb.setOrder(order);
            cb.showOrder();
        }
    }


    i dont know why is it happening like that....

    if i enter an order that is larger than or equal to 20 the program works fine...
    but i made a condition that IF the ORDER is TOO SMALL..
    then the program will ask if you wish to re-order..(i defined a method for that as you can see in my class).




    int this part of the method reOrder()

            System.out.print("\n");
            System.out.print("Do You Wish To Order Again? ");
            String response = br.readLine();
     
            if ((response.equalsIgnoreCase("Y")) || (response.equalsIgnoreCase("Yes"))) {
     
                numberOfOrders = 0;
     
                System.out.print("\n");
                System.out.print("Enter Your New Order ");
                numberOfOrders = Integer.parseInt(br.readLine()); // this part will receive a new value
     
                setOrder(numberOfOrders); // and the new value will be passed on the setOrder() method
                return true;
            }

    supposedly if i re-enter a proper value (which is greater than or equal to 20) ,, the program should stop..
    but why is it asking me over and over again?.... please PLEASE PLEASE HELP ME WITH THIS!!
    Last edited by chronoz13; December 15th, 2009 at 10:51 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can Define what is this error

    You seem to be getting variable clashes...notice that the parameter name of setOrder is the same as the instance variable 'numberOfOrders'. This can cause conflicts between the two, and your do/while loop evaluates the parameter and not the instance variable.

    while((orderAgain == true) && ([COLOR="Red"]this.[/COLOR]numberOfOrders < 20));

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Can Define what is this error

    but im passing a value again on that parameter... supposedly it should change....


    and when i add an ouput statement inside the do-while loop to display that parameter's value

    i notice that after the first value its value is never changing again,.,,


    example: i entered 10, then the program will ask me to re-enter again.....
    and when i enter for example 100: the value of that paremeter is still 10...

    why??...

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can Define what is this error

    Quote Originally Posted by chronoz13 View Post
    and when i add an ouput statement inside the do-while loop to display that parameter's value

    i notice that after the first value its value is never changing again,.,,


    example: i entered 10, then the program will ask me to re-enter again.....
    and when i enter for example 100: the value of that paremeter is still 10...

    why??...
    If you are referring to the line:
     System.out.println(numberOfOrders);// why is the value of this PARAMETER stuck in the first value that had been passed to it? (why does it never change?)

    The parameter value numberOfOrders isn't changed...but the variable this.numberOfOrders does change. Try:

    System.out.println([COLOR="Red"]this.[/COLOR]numberOfOrders);

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Can Define what is this error

    ahhh ...

    but sir copeg...

    im a bit confused about here....

    please help me understand this one...

    ill state the things that make me confuse...


    --- if i enter a value (ex. 10) ..from my main method... i will pass that value on the
    setOrder()'s parameter(numberOfOrders) .... now in my class (CoffeeBeans),
    the value of the parameter of the setOrder method will be assigned into the data member (this.numberOfOrders), then the program will respond because of the condtion
    if ((this.numberOfOrders < 20) && (this.numberOfOrders >= 5)) {
    "Too Small"

    ofcourse the program will ask me if i want to re-order , and if i enter "Yes" or "Y"
    then i will enter a new value using the method reOrder(),
    so in this part
    if ((response.equalsIgnoreCase("Y")) || (response.equalsIgnoreCase("Yes"))) {
     
                numberOfOrders = 0;
     
                System.out.print("\n");
                System.out.print("Enter Your New Order ");
                numberOfOrders = Integer.parseInt(br.readLine());
     
                setOrder(numberOfOrders);
                return true;
            }
    i'm using the data member numberOfOrders to be an input variable, and ITS VALUE will be PASSED
    again into the setOrder()'s parameter..

    now im confuse why is the value of the parameter numberOfOrders of the setOrder() method doesnt change in the second pass or after another pass? even if i enter a valid value (ex.102)and pass it on the setOrder() method again?
    it stuck on the first passed value from the main method.. why?...

    i hope you understand my point sir...

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can Define what is this error

    I think I see what your question is...
    now im confuse why is the value of the parameter numberOfOrders of the setOrder() method doesnt change in the second pass or after another pass
    It does change, but the way your program runs the output makes it look different. How about drawing out the program stack trace as it runs. This is sketched based upon an invalid first entry and valid second entry, and is based upon the code originally posted (I hope this makes sense)
    setOrder(//outputs invalid entry//) 
    setOrder() -> reOrder()
    setOrder() -> reOrder() -> setOrder(//exits normally, no output//)
    setOrder() -> reOrder()
    setOrder(//with invalid, continues loop -]outputs original value//) 
    setOrder() -> reOrder()
    Note that in the case of an invalid entry followed by a valid entry, the way the original code was written the initial setOrder will never exit, and its parameter does not change, while the second pass of setOrder will not exit normally with no output

  7. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (December 16th, 2009)

  8. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Can Define what is this error

    ahhh so parameter's is not just as simple as variable that can change in some execution or pass?...
    hmmmm tnx for that si copeg.... sori to bother you so much hehehehehe

  9. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Can Define what is this error

    sir copeg one more thing.... in my MAIN method...

    this part doest not yet stopped ryt?
    cB.setOrder(order)..

    so the variable order which has the first VALUE (ex.10) is still passing? because my main method doesnt stop yet...

    SO THATS THE ORIGINAL VALUE.!? so NO matter WHAT i DO in my Class (CofeeBeans).. THE VALUE OF <order> in main is still the one THAT THE setOrder() is RECOGNIZED?


    does it have anything to do with the passing inside the CoffeeBeans class.?
    am i going close?


    hehehehehehhe

  10. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can Define what is this error

    Quote Originally Posted by chronoz13 View Post
    this part doest not yet stopped ryt?
    cB.setOrder(order)..
    With an invalid entry, this function call will not return (eg it'll keep running through your loop regardless of what you enter

    so the variable order which has the first VALUE (ex.10) is still passing? because my main method doesnt stop yet...

    SO THATS THE ORIGINAL VALUE.!? so NO matter WHAT i DO in my Class (CofeeBeans).. THE VALUE OF <order> in main is still the one THAT THE setOrder() is RECOGNIZED?

    does it have anything to do with the passing inside the CoffeeBeans class.?
    Kind of...for an invalid entry, you call setOrder which indirectly calls setOrder again with the new value. However the original call to setOrder is still looping with the same value.

    Your code is an example of recursion - the setOrder function is calling itself (albeit indirectly)

  11. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (December 18th, 2009)

  12. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Can Define what is this error

    so thats! is! thats what the thing that i haven't noticed....

    the frist call of the setOrder() method was done inside my MAIN... unfortunately my main doesnt stop
    yet.... hahahah tnx sir copeg!

Similar Threads

  1. ERROR, I CANT DEFINE IT
    By chronoz13 in forum AWT / Java Swing
    Replies: 7
    Last Post: December 2nd, 2009, 09:47 AM