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: ");
    }
}

}

7
  • Have you tried enclosing that whole code in a while(true) loop and use break when the condition has been met? Commented Sep 9, 2016 at 4: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? Commented Sep 9, 2016 at 4: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 Commented Sep 9, 2016 at 4: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. Commented Sep 9, 2016 at 4: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 Commented Sep 9, 2016 at 4:08

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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: ");
     }
 }

1 Comment

Just be careful in using Scanner.in multiple times. Read stackoverflow.com/questions/13102045/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.