Results 1 to 3 of 3
  1. #1
    jayfaxva is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Need help with Loop Problem

    From line 30-48, I need the program to loop continuously if the user doesn't input either 1 or 2 for their insurance type. I need help figuring out exactly what I'm doing incorrectly. I've looked on the internet at examples and I just can't figure out the exact code for it.

    Java Code:
    /**
     * @author 
     */
    import javax.swing.JOptionPane;
    
    public class CarInsurePlan
    {
    	public static void main(String[] args)
    	{
    		String fullName = "";
    		String licenseNum = "";
    		String insuranceType = "";
    		String yourAge = "";
    		String otherInsurance = "";
    		double total = 0;
    		double finalQuote = 0;
    		int numOfTickets = 0;
    		int custNum = 0; //Set number of customers to 0
    		int x; //Set x to indexOf (',')
    		
    		do{
    			//Do-while loop. The program will keep looping until the user enters in a comma between his/her name
    			do{
    			fullName = JOptionPane.showInputDialog(null, "Please enter your full name like the following example: FirstName,LastName (Stephen,King)");
    			x=fullName.indexOf(',');
    			}while (x == -1);
    			
    			licenseNum = JOptionPane.showInputDialog(null, "What is your license number?");
    			
    			do{
    			String input = JOptionPane.showInputDialog(null, "What type of insurance do you have?" + " 1 for Basic($55.10), 2 for Premium($75.50)");
    			int insuranceChoice = Integer.parseInt(input);
    			if (insuranceChoice == 1)
    			{
    				total = total + 55.10;
    				insuranceType = "Basic";
    			}
    			else if (insuranceChoice == 2)
    			{
    				total = total + 75.50;
    				insuranceType = "Premium";
    			}
    			else
    			{
    			//If the user doesn't choose 1 or 2 of the packages, he/she will be re-prompted to choose either 1 or 2
    			JOptionPane.showMessageDialog(null, "Please select 1(Basic) or 2(Premium)");
    			JOptionPane.showInputDialog(null, "What type of insurance do you have?" + " 1 for Basic($55.10), 2 for Premium($75.50)");
    			}while (insuranceChoice != 1 || insuranceChoice != 2);
    			
    			if(JOptionPane.showConfirmDialog(null,"Are you under 25 years of age?")== JOptionPane.YES_OPTION)
    			{
    				total = total + 10.00;
    				yourAge = "Yes";
    			}
    			else
    			{
    				yourAge = "No";
    			}
    			if(JOptionPane.showConfirmDialog(null,"Do you have any speeding tickets?")== JOptionPane.YES_OPTION)
    			{
    				input = JOptionPane.showInputDialog(null, "How many tickets do you have?");
    				numOfTickets = Integer.parseInt(input);
    				total = total + (numOfTickets *5);
    			}
    			else
    			{
    				numOfTickets = 0;
    			}
    			if(JOptionPane.showConfirmDialog(null,"Do you currently hold any other car insurance?")== JOptionPane.YES_OPTION)
    			{
    				total = total - 10.00;
    				otherInsurance = "Yes";
    			}
    			else 
    			{
    				total = total + 20.00;
    				otherInsurance = "No";
    			}
    			finalQuote = total * 6;
    			//Adds 1 to the customer count once the customer is finished
    			custNum++;
    			//Use substrings to display the first name and last name of the customer. (0,x) will start at the letter of the first name and stop before the comma. (x+1 will read the string after the comma
    			JOptionPane.showMessageDialog(null, "First Name: " + fullName.substring(0,x) + "\nLast Name: " + fullName.substring(x+1)+ "\nLicense number: " + licenseNum + "\nInsurance type: " + insuranceType +
    			"\nUnder 25 years old?: " + yourAge + "\nNumber of speeding tickets: " + numOfTickets + "\nOther Insurance: " +  otherInsurance +
    			"\nTotal: $" + total + "\n6 month quote: $" + finalQuote);
    		}while (custNum <=10);//Will loop until it finishes 10 customers
    	}
    }

  2. #2
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Default Re: Need help with Loop Problem

    <code>
    do
    {
    insuranceChoice = 0;
    String input = JOptionPane.showInputDialog(null, "What type of insurance do you have? \r\n1 for Basic($55.10), 2 for Premium($75.50)");
    insuranceChoice = Integer.parseInt(input);
    if(insuranceChoice == 1)
    {
    total = total + 55.10;
    insuranceType = "Basic";
    }
    else if (insuranceChoice == 2)
    {
    total = total + 75.50;
    insuranceType = "Premium";
    }
    }while (insuranceChoice != 1 & insuranceChoice != 2);

    you have to change the || operator to & and then remove the else part,
    then works fine....!,,,
    ThanX
    Rj

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Need help with Loop Problem

    You should spend a little more effort explaining why the code was wrong, what it was doing and why/how your code works. Give giving code is not as useful as explaining why and how.

    Also can you explain why you used the bitwise AND & vs the logical AND &&
    Last edited by Norm; 11-21-2011 at 04:35 PM.

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 03:25 AM
  2. problem with loop (maybe?)
    By jdg951 in forum New To Java
    Replies: 7
    Last Post: 12-08-2010, 11:32 AM
  3. Problem with a for loop
    By ile4 in forum New To Java
    Replies: 3
    Last Post: 12-02-2010, 03:23 PM
  4. Problem with For Loop
    By dave12har in forum New To Java
    Replies: 5
    Last Post: 11-30-2010, 04:35 PM
  5. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •