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.
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
}
}
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....!,,,
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 &&