• Some help with java do whiles and char comparing
    6 replies, posted
[code] import java.util.Scanner; public class vehicle_insurance { public static void main(String[] args) { Scanner iireader = new Scanner(System.in); String pnum, phol, regnum, vmodel, phrate = ""; char cpouts; int yman, nppce, odr, pamnt, phage, yinfr, yacci; float adprate, ddprate, adpamnt, ddpamnt, cmval; double saprem = 0.0, srprem = 0.0, abpamnt, adjpamnt, acpamnt, bcpamnt; System.out.println("*** Insurance Calculator ***\n"); // Policy Number Input System.out.println("Enter Policy Number:"); pnum = iireader.nextLine(); // Policy Holder Input System.out.println("Enter Policy Holder:"); phol = iireader.nextLine(); // Vehicle Reg Input System.out.println("Enter Vehicle Registration Number:"); regnum = iireader.nextLine(); // Vehicle Model Input System.out.println("Enter Vehicle Make/Model:"); vmodel = iireader.nextLine(); // Vehicle Manufacturer Input System.out.println("Enter Year of Manufacture:"); yman = iireader.nextInt(); // Purchase Price Input System.out.println("Enter New Purchase Price:"); nppce = iireader.nextInt(); // Odometer Reading Input System.out.println("Enter Odometer Reading: "); odr = iireader.nextInt(); // Policy Holder Age Input System.out.println("Enter policy holder age: "); phage = iireader.nextInt(); // 5 Year Infringements Input System.out.println("Enter number of traffic infringements received in previous 5 years: "); yinfr = iireader.nextInt(); // 5 Year Accidents Input System.out.println("Enter number of 'at fault' accidents in the previous 5 years: "); yacci = iireader.nextInt(); System.out.println("\n*** Previous Claim Payout Entry ***\n"); do { System.out.println("A - Add Accident Claim Payout"); System.out.println("B - Add Breakdown Claim Payout"); System.out.println("X - End Claim Payout Entry\n"); System.out.println("Enter your selection: "); cpouts = iireader.next().charAt(0); if (!"A".equals(cpouts) || !"B".equals(cpouts) || !"X".equals(cpouts)) { System.out.println("Invalid option entered - please try again.\n"); } else if ("A".equals(cpouts)) { System.out.println("Enter new accident claim payout amount: "); acpamnt = iireader.nextInt(); } else if ("B".equals(cpouts)) { System.out.println("Enter new breakdown claim payout amount: "); bcpamnt = iireader.nextInt(); } } while(!"X".equals(cpouts)); System.out.println("*** Policy Information ***\n"); System.out.println("Policy Number: " + pnum); System.out.println("Policy Holder: " + phol); System.out.println("Vehicle Registration: " + regnum + "\n"); System.out.println("Vehicle Make / Model: " + vmodel); System.out.println("Manufacture Date: " + yman); System.out.println("\n*** Base Premium Calculation ***\n"); // Defining age and distance depreciation variables adprate = ((2015 - yman) + 1) * (5f / 100f); ddprate = (odr / 10000f) * (2f / 100f); adpamnt = Math.round(adprate * nppce); ddpamnt = Math.round(ddprate * nppce); cmval = nppce - ((int)adpamnt + (int)ddpamnt); pamnt = (int)(cmval * (5f / 100f)); System.out.println("New Purchase Price: $ " + nppce); System.out.println("Age Depreciation Amount: $ -" + adpamnt); System.out.println("Distance Depreciation Amount: $ -" + ddpamnt); System.out.println("Estimated Current Market Value: $ " + cmval + "\n"); System.out.println("Base Premium Amount: $ " + pamnt); System.out.println("\n*** Adjustments to Premium Calculation ***\n"); // Defining Surcharge to Premium Depending on age if (phage < 25) { saprem = (int)(pamnt * (50f / 100f)); } else if (phage >= 25 && phage <= 34) { saprem = (int)(pamnt * (20f / 100f)); } else if (phage >= 35) { saprem = 0; } abpamnt = saprem + pamnt; // Defining Policy Holder Rating Depending on Infringements and Accidents in the past 5 years if ((yinfr == 0) && (yacci == 0)) { phrate = "Good"; srprem = (abpamnt * (10f / 100f)); } else if ((yinfr >= 1 && yinfr <= 2) && (yacci == 0)) { phrate = "Average"; srprem = (abpamnt * (10f / 100f)); } else if ((yinfr >= 3) && (yacci >= 1)) { phrate = "Poor"; srprem = (abpamnt * (30f / 100f)); } adjpamnt = (saprem - srprem) + pamnt; System.out.println("Policy Holder Age: " + phage); System.out.println("Policy Holder Rating: " + phrate); System.out.println("Age-based premium adjustment: $ " + saprem); System.out.println("Rating-based premium adjustment: $ -" + (double) (Math.round(srprem*100)/100) + "\n"); System.out.println("Adjusted Premium Amount: $ " + (double) (Math.round(adjpamnt*100)/100)); } } [/code] This is my code I have, For some reason the do while statement always returns "Invalid option entered - please try again." no matter what I enter. I have tried everything I can think of, Please Help
The variable cpouts is declared as a char, but you seem to be comparing string values to it. Double quotes == string, single quotes == char.
charAt returns a character, not a String. "A" != 'A' [editline]5th October 2015[/editline] Ninja'd
Thankyou guys ill see if it works! [editline]5th October 2015[/editline] [code] do { System.out.println("A - Add Accident Claim Payout"); System.out.println("B - Add Breakdown Claim Payout"); System.out.println("X - End Claim Payout Entry\n"); System.out.println("Enter your selection: "); cpouts = iireader.next().charAt(0); if (cpouts != 'A' || cpouts != 'B' || cpouts != 'X') { System.out.println("Invalid option entered - please try again.\n"); } else if (cpouts == 'A') { System.out.println("Enter new accident claim payout amount: "); acpamnt = iireader.nextInt(); } else if (cpouts == 'B') { System.out.println("Enter new breakdown claim payout amount: "); bcpamnt = iireader.nextInt(); } } while(cpouts == 'X'); [/code] This is what I changed it to and this is what is printing to the console once I input [code] *** Previous Claim Payout Entry *** A - Add Accident Claim Payout B - Add Breakdown Claim Payout X - End Claim Payout Entry Enter your selection: X Invalid option entered - please try again. A - Add Accident Claim Payout B - Add Breakdown Claim Payout X - End Claim Payout Entry Enter your selection: B Invalid option entered - please try again. [/code] For some reason when I input B it ends it and outputs the next part of the code into the console but it is mean to print "Enter new breakdown claim payout amount:" and get the next input. Once I input X it should end the code and go to the next part which it is also not doing. Please Help
That last line (the while statement) should be a !=, not ==. Also, you should change the first if statement to a simple "else print 'invalid'" at the end, so if you need to add more options you can just add the "else if cpouts == newoption".
Thanks fauxpark, no i just need to address the issue of it not printing "Enter new breakdown claim payout amount:"
[QUOTE=Kirbd;48831771]Thanks fauxpark, no i just need to address the issue of it not printing "Enter new breakdown claim payout amount:"[/QUOTE] That would be the second part of my above post. If you really don't want to/can't change the if statement, the problem is that you're using ||s instead of &&s. When that if statement gets hit, it checks that at least one of those is true (which will happen even on valid input), instead of checking if all of them are false (which will only happen on invalid input). As I said, the best way to handle this is to use an "else if cpouts != 'X' { ... }" at the end to catch invalid input.
Sorry, you need to Log In to post a reply to this thread.