1

I have read numerous posts and pages about while loops and such, I just cannot understand where to place a loop to repeat this until one of the three conditions is met. If someone could just point to the places I need to add a while-do or whatever I would greatly appreciate that, no need to print out any code unless you want to.

    System.out.print("Customer name: ");
    name=scan.next();
    System.out.print("Customer member level: ");
    level=scan.next();
    if (level.equalsIgnoreCase("Jedi"))
    {
        System.out.print("Current balance: ");
        balance=scan.nextDouble();
        System.out.print("Was the payment made late?: ");
        late=scan.next();
        if (late.equalsIgnoreCase("Yes"))
        {
            double interestLatePayment=(balance*0.02);
            paymentToPrinciple=(balance*0.04);
            totalPayment=(interestLatePayment+paymentToPrinciple);
            percentToPrinciple=100*(paymentToPrinciple/totalPayment);
            percentToInterest=(100-percentToPrinciple);

            System.out.println("\nBill for "+level+" customer "+name);
            System.out.printf("Card Balance: $%1.2f%n", balance);
            System.out.printf("Interest for late payment (2.0%% of principle): $%1.2f%n", interestLatePayment);
            System.out.printf("Minimum payment to principle (4%% of principle): $%1.2f%n", paymentToPrinciple);
            System.out.printf("Minimum total payment (payment and fees): $%1.2f%n", totalPayment);
            System.out.printf("Percent to principle: %1.1f%%n", percentToPrinciple);
            System.out.printf("Percent to interest and fees: %1.1f%%n", percentToInterest);
        }
        else if(late.equalsIgnoreCase("No"))
        {
            double interestLatePayment=(balance*0.01);
            paymentToPrinciple=(balance*0.04);
            totalPayment=(interestLatePayment+paymentToPrinciple);
            percentToPrinciple=100*(paymentToPrinciple/totalPayment);
            percentToInterest=(100-percentToPrinciple);

            System.out.println("\nBill for "+level+" customer "+name);
            System.out.printf("Card Balance: $%1.2f%n", balance);
            System.out.printf("Interest for on-time payment (1.0%% of principle): $%1.2f%n", interestLatePayment);
            System.out.printf("Minimum payment to principle (4%% of principle): $%1.2f%n", paymentToPrinciple);
            System.out.printf("Minimum total payment (payment and fees): $%1.2f%n", totalPayment);
            System.out.printf("Percent to principle: %1.1f%%n", percentToPrinciple);
            System.out.printf("Percent to interest and fees: %1.1f%%n", percentToInterest);
        }
    }
    if (level.equalsIgnoreCase("Padowan"))
    {
        System.out.print("Current balance: ");
        balance=scan.nextDouble();
        System.out.print("Was the payment made late?: ");
        late=scan.next();
        if (late.equalsIgnoreCase("Yes"))
        {
            double interestLatePayment=(balance*0.028);
            paymentToPrinciple=(balance*0.04);
            totalPayment=(interestLatePayment+paymentToPrinciple);
            percentToPrinciple=100*(paymentToPrinciple/totalPayment);
            percentToInterest=(100-percentToPrinciple);
            lateFee=10;

            System.out.println("\nBill for "+level+" customer "+name);
            System.out.printf("Card Balance: $%1.2f%n", balance);
            System.out.printf("Interest for late payment (2.8%% of principle): $%1.2f%n", interestLatePayment);
            System.out.printf("Late fee: $%1.2f%n", lateFee);
            System.out.printf("Minimum payment to principle (4%% of principle): $%1.2f%n", paymentToPrinciple);
            System.out.printf("Minimum total payment (payment and fees): $%1.2f%n", totalPayment);
            System.out.printf("Percent to principle: %1.1f%%n", percentToPrinciple);
            System.out.printf("Percent to interest and fees: %1.1f%%n", percentToInterest);
        }
        else if(late.equalsIgnoreCase("No"))
        {
            double interestLatePayment=(balance*0.018);
            paymentToPrinciple=(balance*0.04);
            totalPayment=(interestLatePayment+paymentToPrinciple);
            percentToPrinciple=100*(paymentToPrinciple/totalPayment);
            percentToInterest=(100-percentToPrinciple);

            System.out.println("\nBill for "+level+" customer "+name);
            System.out.printf("Card Balance: $%1.2f%n", balance);
            System.out.printf("Interest for late payment (1.8%% of principle): $%1.2f%n", interestLatePayment);
            System.out.printf("Minimum payment to principle (4%% of principle): $%1.2f%n", paymentToPrinciple);
            System.out.printf("Minimum total payment (payment and fees): $%1.2f%n", totalPayment);
            System.out.printf("Percent to principle: %1.1f%%n", percentToPrinciple);
            System.out.printf("Percent to interest and fees: %1.1f%%n", percentToInterest);
        }
    }
    if (level.equals("Youngling"))
    {
        System.out.print("Current balance: ");
        balance=scan.nextDouble();
        System.out.print("Was the payment made late?: ");
        late=scan.next();
        if (late.equalsIgnoreCase("Yes"))
        {
            double interestLatePayment=(balance*0.035);
            paymentToPrinciple=(balance*0.04);
            totalPayment=(interestLatePayment+paymentToPrinciple);
            percentToPrinciple=100*(paymentToPrinciple/totalPayment);
            percentToInterest=(100-percentToPrinciple);
            lateFee=20;

            System.out.println("\nBill for "+level+" customer "+name);
            System.out.printf("Card Balance: $%1.2f%n", balance);
            System.out.printf("Interest for late payment (3.5%% of principle): $%1.2f%n", interestLatePayment);
            System.out.printf("Late fee: $%1.2f%n", lateFee);
            System.out.printf("Minimum payment to principle (4%% of principle): $%1.2f%n", paymentToPrinciple);
            System.out.printf("Minimum total payment (payment and fees): $%1.2f%n", totalPayment);
            System.out.printf("Percent to principle: %1.1f%%n", percentToPrinciple);
            System.out.printf("Percent to interest and fees: %1.1f%%n", percentToInterest);
        }
        else if(late.equalsIgnoreCase("No"))
        {
            double interestLatePayment=(balance*0.025);
            paymentToPrinciple=(balance*0.04);
            totalPayment=(interestLatePayment+paymentToPrinciple);
            percentToPrinciple=100*(paymentToPrinciple/totalPayment);
            percentToInterest=(100-percentToPrinciple);

            System.out.println("\nBill for "+level+" customer "+name);
            System.out.printf("Card Balance: $%1.2f%n", balance);
            System.out.printf("Interest for late payment (2.5%% of principle): $%1.2f%n", interestLatePayment);
            System.out.printf("Minimum payment to principle (4%% of principle): $%1.2f%n", paymentToPrinciple);
            System.out.printf("Minimum total payment (payment and fees): $%1.2f%n", totalPayment);
            System.out.printf("Percent to principle: %1.1f%%n", percentToPrinciple);
            System.out.printf("Percent to interest and fees: %1.1f%%n", percentToInterest);
        }
    }
    else 
    {
        System.out.print("Invalid member level. Enter valid member level: ");
    }
}

}

earthw0rmjim
  • 17,394
  • 8
  • 45
  • 61
  • Have you tried enclosing that whole code in a while(true) loop and use break when the condition has been met? – Aaron Sep 09 '16 at 04:03
  • Also, "until desired input is received" what is the desired input for the cases you are concerned about? There's several calls to Scanner here, which one are you having trouble with? – markspace Sep 09 '16 at 04:04
  • the problem is that i have three conditions that can be met, and i only need one to go and then stop the loop. so i dont know where to place it if thats what i should do –  Sep 09 '16 at 04:06
  • 1
    One thing you can try is to break the code up into methods. It's one huge blob right now; I'm having trouble figuring out what you're doing. Take each of those `if` cases and make them a separate method. I think it'll will be easier for you and us both to see what needs to be fixed then. – markspace Sep 09 '16 at 04:07
  • my error right now is that running this yields up to the first if and if thats not met it prints the line at the bottom and has no loop at that point –  Sep 09 '16 at 04:08
  • Just enclose your whole code with while, and add breaks inside the blabla.equalsIgnoreCase("Yes") (and no) condition. Assuming that I understand your question correctly. – Aaron Sep 09 '16 at 04:10
  • That's what I'd suppose too. Wrap the whole code presented in one while block and at the end of the if-blocks, which are your triggers for reaching the goal, you put break; – Lynx 242 Sep 09 '16 at 04:18

2 Answers2

1

Something like this:

String input;
do {
  input = getNewInput(); //replace this with however you get input, probably a scanner
}while (!input.equals("firstcondition") && !input.equals("secondcondition"))

//then after that do whatever you want to with the input now that you know it matches
nhouser9
  • 6,572
  • 3
  • 18
  • 39
0

Something like this. You have to figure out yourself, which if-results are your triggers for stopping this.

 while(true){
     System.out.print("Customer name: ");
     name=scan.next();
     System.out.print("Customer member level: ");
     level=scan.next();
     if (level.equalsIgnoreCase("Jedi"))
     {

         if (late.equalsIgnoreCase("Yes"))
         {
             // your code
             break;
         }
         else if(late.equalsIgnoreCase("No"))
         {
             // your code
         }
     }
     if (level.equalsIgnoreCase("Padowan"))
     {
         if (late.equalsIgnoreCase("Yes"))
         {
             // your code
             break;
         }
         else if(late.equalsIgnoreCase("No"))
         {
            // your code
         }
     }
     if (level.equals("Youngling"))
     {

         if (late.equalsIgnoreCase("Yes"))
         {
             // your code
             break;
         }
         else if(late.equalsIgnoreCase("No"))
         {
             // your code
         }
     }
     else 
     {
         System.out.print("Invalid member level. Enter valid member level: ");
     }
 }
Lynx 242
  • 7,658
  • 5
  • 22
  • 40
  • Just be careful in using Scanner.in multiple times. Read http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo – Aaron Sep 09 '16 at 04:34