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
Code :
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
Code :
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()
Code :
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!!
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.
Code :
while((orderAgain == true) && ([COLOR="Red"]this.[/COLOR]numberOfOrders < 20));
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??...
Re: Can Define what is this error
Quote:
Originally Posted by
chronoz13
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:
Code :
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:
Code :
System.out.println([COLOR="Red"]this.[/COLOR]numberOfOrders);
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
Code :
if ((this.numberOfOrders < 20) && (this.numberOfOrders >= 5)) {
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
Code :
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... :( :confused:
Re: Can Define what is this error
I think I see what your question is...
Quote:
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)
Code :
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
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
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 :)
Re: Can Define what is this error
Quote:
Originally Posted by
chronoz13
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
Quote:
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)
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! :)