In my method i want user input if it passes first while it need to go to second while loop, in second while if the condition fails it need to re-run the loop but instead of re-running itself, the first while is being executed.
//Method to read, validate and store postcode and purchase amount for N customers
public void addRecord(int customerCounter) throws IOException
{
int postcode;
double purchaseAmount;
int conform =0;
do{
BufferedReader breader= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Postcode: ");
postcode= Integer.parseInt(breader.readLine());
if(4121 >postcode)
{
System.out.print("Postcode must be greater than 4121");
System.out.print("Try again: ");
}
else if(postcode > 4123)
{
System.out.print("Postcode must be less than 4123");
System.out.print("Try again: ");
}
else
{
conform = 1;
//System.out.println(conform);
}
}
while(4121 >postcode && postcode > 4123);
if(conform == 1)
{
System.out.println(conform);
do
{
BufferedReader breader2= new BufferedReader(new InputStreamReader(System.in));
breader2= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Purchase Amount: ");
purchaseAmount= Double.parseDouble(breader2.readLine());
if(purchaseAmount < 60)
{
System.out.print("Purchase Amount must be greater than $60");
System.out.print("Try again: ");
continue;
}
else if(purchaseAmount > 500)
{
System.out.print("Purchase Amount must be greater than $500");
System.out.print("Try again: ");
}
}
while(purchaseAmount < 60 && purchaseAmount > 500);
}
}
Your conditions in the while
are wrong, they will always be false
postcode = 5000;
while(postcode < 4121 && postcode > 4123); -> while(false && true); -> false
postcode = 3000;
while(postcode < 4121 && postcode > 4123); -> while(true && false); -> false
Use OR instead of AND
while(postcode < 4121 || postcode > 4123);
Same goes for the second while.
The second execution of the first while is probably because you call addRecord
again.
The best solution is to use 2 nested infinite loops.
In this way the program checks only 2 times the postcode and the price inserted for each loop to determine if can go forward, instead of 4 times in the your solution.
//Method to read, validate and store postcode and purchase amount for N customers
public void addRecord(int customerCounter) throws IOException
{
int postcode;
double purchaseAmount;
int conform =0;
do{
BufferedReader breader= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Postcode: ");
postcode= Integer.parseInt(breader.readLine());
if(4121 >postcode)
{
System.out.print("Postcode must be greater than 4121");
System.out.print("Try again: ");
}
else if(postcode > 4123)
{
System.out.print("Postcode must be less than 4123");
System.out.print("Try again: ");
}
else
{
System.out.println(conform);
do{
BufferedReader breader2= new BufferedReader(new InputStreamReader(System.in));
breader2= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Purchase Amount: ");
purchaseAmount= Double.parseDouble(breader2.readLine());
if(purchaseAmount < 60)
{
System.out.print("Purchase Amount must be greater than $60");
System.out.print("Try again");
continue;
}
else if(purchaseAmount > 500)
{
System.out.print("Purchase Amount must be greater than $500");
System.out.print("Try again");
}else{
break;
}
}while(true);
break;
}
}
while(true);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.