do while loop - inside a do - while which will take effect in a If statement
Hey , i know i solved the previous problem but another problem emerges with running a do while inside a do while inside a if statement. let me make more sense of this for u.
do the program , while the error is non existant.
if user does not want to repeat code , then return to beginning
Here is a code representation of my problem.
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class foodtest {
public static String error = "";
public static String returning = "";
public static void main(String[] args) throws IOException
{
do {
System.out.println("what would you like?");
System.out.println("");
System.out.println("beans or carrots");
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader r = new BufferedReader(input);
String option = r.readLine();
if (option.equalsIgnoreCase ("beans"))
{
System.out.println("how many beans do you want?");
System.out.println("");
System.out.println("one");
System.out.println("two");
String beans = r.readLine();
if (beans.equalsIgnoreCase ("one"))
{
System.out.println("there is " + beans + " bean");
}
else if (beans.equalsIgnoreCase ("two"))
{
System.out.println("here is " + beans + " beans");
}
}
else if (option.equalsIgnoreCase ("carrots"))
{
System.out.println("how many carrots do you want?");
System.out.println("");
System.out.println("one");
System.out.println("two");
String carrots = r.readLine();
if (carrots.equalsIgnoreCase ("one"))
{
System.out.println("here is " + carrots + " carrot");
}
else if (carrots.equalsIgnoreCase ("two"))
{
System.out.println("here is " + carrots + " carrot");
System.out.println("");
//*******HERE IS THE PROBLEM**************
System.out.println("would you like to return to the start. Enter Y or N");
returning = r.readLine();
if (return.equalsIgnoreCase("Y)")
{
// HERE I NEED TO GET BACK TO THE START
}
if (return.equalsIgnoreCase("N)")
{
System.out.println("Thank you for trying.");
}
}
}
else
{
System.out.println("Invalid entry has occured, would you like to try again? Enter Y or N");
error = r.readLine();
// } **i thought do would end here, but this will just end the else**
if (error.equalsIgnoreCase ("Y"))
{
// ***** RETURN TO START ??? *****}
}
if (error.equalsIgnoreCase ("N"))
{
System.out.println("Thank you for trying");
}
}
} // END DO
while (error.equalsIgnoreCase("Y"));
}
}
Not sure how to put a do - while inside a if statement with a do - while already covering the code!
Help would be appreciated :) ! thanks