'Setting up while and do-while loops from user input questions

I have to create a code used to determine the amount of insurance payout. I have to use a while loop after the initial prompt asking if they want to get an analysis. Then I need to use nested do-whiles, 2 do-while loops, in the prompts and if-else-ifs. I need help with the loops.

`

public class Main
    {
     public static void main(String[] args) {
    Scanner input = new Scanner (System.in);
    String answer = " ";
    String cont = ("%nMUTUALLY ACCIDENTAL, INC." +
     "%n%nDo you want an analysis of earthquake coverage" +
     "for your property? Enter 'Y' or 'N': ");

        System.out.printf (cont);
        (Scanner (answer = input.nextLine ()));

    while (answer.equalsIgnoreCase ("Y"))
        System.out.printf ("%nMUTUALLY ACCIDENTAL, INC." +
      "%n%nEarthquake coverage analyzer" +
      "%nPlease enter your name:  ");
    do
    System.out.printf("%nPlease enter the insured value of your home:  ");
    System.out.printf("%nRichter Scale     Damage Assessment"+
                   "     9.0+         Total destruction."+
                   "     8.0          Most structures dell."+
                   "     7.0          Many buildings destroyed"+
                   "     6.0          Many buildings considerably damaged, some collapsed."+
                   "     4.5          Damage to poorly constructed buildings."+
                   "     3.5          Felt by many people, no destruction."+
                   "     0            Generally not felt by people."+
                  "%n%nPlease enter the Richter scale value for the earthquake: ");
    do 
    system.out.printf("%n“Your Name: “,Xxxxxxxxxx Xxxxxxxxxxxxxxx
                        “Home’s Insured Vale: “,$ZZZ,ZZZ,ZZZ9.99
                        “Richter Scale: “,Z9.99 or -Z9.99

                        “Is this information correct? ‘Y’ or ‘N’: “);
    System.out.printf("%nDo you have another property? Enter 'Y' or 'N':  ");
    coverage = String.format(%n%nPAYOUT FOR EARTHQUAKE "+
                               "DAMAGE"+
                             "%N%NHOMEOWNER: %S"+
                             "$n$nDATE:  %tD"+
                             "%nTime:  %tr%n, insured, dateTime, dateTime);
    coverage +=String.format("%n%-52s %4s $%,20.2f"+
                             "%nDeductible $47 $,20,2f"+
                             "%n46s TOTAL %4s $%,20.2f%n", message, " ", payout, " ", deductible, " ", " ", payout + deductible);

       System.out.print(coverage) when richter > 0
                             
                             exit main()
      
      else 
      System.out.printf("%nThank you for using the Earthquake" +
       "Coverage Analyzer.");
  }
}


Solution 1:[1]

I can't see a clear question in here so I assume you are stuck on the structure of the loops.

while has the following structure:

// before
while (condition) {
    statements;
    statements;
}
//after

It first tests the condition. if true, it runs the statements. if the condition is false it skips straight to 'after'

do-while has the following structure:

// before
do {
    statements;
    statements;
} while (condition);
// after

It first runs the statements. then it tests the condition. if the condition is true it goes back into 'do' and runs the statements again. if the condition is false it goes to after.

You can nest loops like so:

while (someCondition) {
    int i = 1;
    String s = "";
    // other code, just filling this to make it look a bit better
    while (otherCondition) {
        int j = i + 3; // i is available because this while is inside the block of the other while
    }
}

You can do the same with do-while / mix them.

One additional thing: if you use break, you exit the loop you are in. if you have nested loops then you will break from the deepest loop.

while (someCondition) {
    while (otherCondition) {
        break; // goes to after-inner-while
    }
    // after-inner-while
}

sometimes you want to jump out of the outer while loop. break can help with this if you use labels. you can name loops like so:

outer: while (someCondition) {
     inner: while (otherCondition) {
         break outer; // goes to after-outer-while
     }
     // after-inner-while
}
// after-outer-while

this all also works for for-loops but you specifically asked questions about while loops.

be very careful about not using blocks after your while. this is allowed but will be hard to read / a source of bugs:

while (someCondition)
    doSomething();

it can easily be confuse you into thinking your code works differently than it actually does:

while (someCondition)
    doSomething();
    doAnotherThing();

the above only runs doSomething inside the loop but someone reading your code will likely think both method calls will be run.

Now to the code you posted:

I think you are expected to do something along the lines of:

boolean answeredYes = false;
while (!answeredYes) {
    // ask for user input
    // if user input is "Y" set answeredYes to true
} 

since you say you need to use a while loop first. I would prefer do-while for this since you can do:

do {
    // ask for user input
} while (userinput not what you expect);

My best advice right now is: don't try to write the entire method. Write code for one problem at a time. Start with the Y/N question and what you are supposed to do if the user types Y or N

then move on to the next step and work out what to do and how to do it. Write yourself comments (notes) that let you know what your intentions are.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joeblade