troubleshooting Question

While loop

Avatar of professorchaos14
professorchaos14 asked on
Programming Languages-OtherJava
2 Comments1 Solution587 ViewsLast Modified:
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;
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros