Avatar of professorchaos14
professorchaos14
 asked on

While loop

I've created a java program that calculates insurance premiums. I have a menu selection issue with my while loops. I currently have 4 while statements, 1 to ensure that only A, B or X can be pressed, 1 to instruct what happens when A is pressed,  1 to instruct what happens when B is pressed, and 1 to instruct what happens when X is pressed. Currently however once one of the while statements has been fulfilled it continues with the code and no longer includes the previous while loop. I think I need nested while loops but I'm not sure of the format. The loop needs to continue in any order until X is pressed. My current affected code is as follows:

             
            
System.out.println("*** Previous Claim Payout Entrey ****");
		
		System.out.println();
		
		System.out.println("A - Add Accident Claim Payout");
		System.out.println("B - Add Breakdown Claim Payout");
		System.out.println("X - End Claim Payout Entry");
		
		System.out.println();
		
		System.out.print("Enter your selection: ");
		claims = console.next().toUpperCase();
		
		
		 while ("ABX".indexOf(claims) < 0)
		{
			System.out.println("Invalid option entered - please try again.");
			
			System.out.println();
			
			System.out.println("A - Add Accident Claim Payout");
			System.out.println("B - Add Breakdown Claim Payout");
			System.out.println("X - End Claim Payout Entry");
			
			System.out.println();
			
			System.out.print("Enter your selection: ");
			claims = console.next().toUpperCase();
		}
			
		while ("A".indexOf(claims) >= 0)
		{
			System.out.print("Enter new accident claim payout amount: ");
			claim1 = console.nextDouble();
			
			System.out.println();
			
			System.out.println("A - Add Accident Claim Payout");
			System.out.println("B - Add Breakdown Claim Payout");
			System.out.println("X - End Claim Payout Entry");
			
			System.out.println();
			
			System.out.print("Enter your selection: ");
			claims = console.next().toUpperCase();
			List1.add(claim1);
		}
		
		while ("B".indexOf(claims) >= 0)
		{
			System.out.print("Enter new breakdown claim payout amount: ");
			claim2 = console.nextDouble();
			
			System.out.println();
			
			System.out.println("A - Add Accident Claim Payout");
			System.out.println("B - Add Breakdown Claim Payout");
			System.out.println("X - End Claim Payout Entry");
			
			System.out.println();
			
			System.out.print("Enter your selection: ");
			claims = console.next().toUpperCase();
			List2.add(claim2);
		}
		
		while ("X".indexOf(claims) >= 0)
		{
			break;
		}

		double sum = 0;
				for (double i : List1)
					sum += i;
		
		double sum1 = 0;
				for (double i : List2)
					sum1 += i;
		
		accidentClaim = (sum / 1000) * 0.01;
		
		breakdownClaim = (sum1 /1000) * 0.02;

Open in new window

JavaProgramming Languages-Other

Avatar of undefined
Last Comment
Am P

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
girionis

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Am P

You can use single loop with if.. else if .. as given below:


while (true) {
	System.out.println();
			
	System.out.println("A - Add Accident Claim Payout");
	System.out.println("B - Add Breakdown Claim Payout");
	System.out.println("X - End Claim Payout Entry");
	
	System.out.println();
			
	System.out.print("Enter your selection: ");
	claims = console.next().toUpperCase();

	if ("X".indexOf(claims) >= 0) {
		break;
	} else if ("ABX".indexOf(claims) < 0) {
		System.out.println("Invalid option entered - please try again.");
	} else if ("A".indexOf(claims) >= 0) {
		System.out.print("Enter new accident claim payout amount: ");
		claim1 = console.nextDouble();
			
		System.out.println();
			
		System.out.println("A - Add Accident Claim Payout");
		System.out.println("B - Add Breakdown Claim Payout");
		System.out.println("X - End Claim Payout Entry");
		
		System.out.println();
		
		System.out.print("Enter your selection: ");
		claims = console.next().toUpperCase();
		List1.add(claim1);
	} else if ("B".indexOf(claims) >= 0) {
		System.out.print("Enter new breakdown claim payout amount: ");
		claim2 = console.nextDouble();
			
		System.out.println();
		
		System.out.println("A - Add Accident Claim Payout");
		System.out.println("B - Add Breakdown Claim Payout");
		System.out.println("X - End Claim Payout Entry");
			
		System.out.println();
			
		System.out.print("Enter your selection: ");
		claims = console.next().toUpperCase();
		List2.add(claim2);
	} 
}

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23