Link to home
Start Free TrialLog in
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

ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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