I'm in an introductory Java class (just started) so I'm pretty bad at it, any advice or help would be greatly appreciated.
I am having trouble comparing two string variables. I am using .equals() for the two variables "batteryOld" and "batteryCompareYes" .
Given that the user inputs "Yes" they should be considered equal and therefore the statement "true" right?
Problem is that instead of going into the if , it fails the condition and goes straight to the else where a system.out prints an error message....
Any way to fix this so that if both are Yes, it goes into the if loop setting the batteryPenalty variable to true?
Thanks for any help!
Here's a the excerpt from the program:
public static double computeChargeRate(int robotPosition){ Scanner input = new Scanner(System.in); double robotWeight = 1; String batteryOld = "Null"; String batteryCompareYes = "Yes"; String batteryCompareNo= "No"; boolean batteryPenalty = true; double chargeRate = 0; //ask the user the robot's weight (assume weight is measured in kg since not otherwise stated) System.out.print("What is the robot's weight in kg? "); robotWeight = input.nextDouble(); //ask the user whether the robot's battery is old or not System.out.print("Is the robot's battery old? Please answer Yes or No. "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { batteryOld = br.readLine(); } catch (IOException ioe) { System.exit(1); } System.out.println(batteryOld); //preform check to see if the robots's battery is old if((batteryOld.equals(batteryCompareYes))){ batteryPenalty = true; } if((batteryOld.equals(batteryCompareNo))){ batteryPenalty = false; } else{ System.out.println("The input for the robot's battery is not applicable- Error."); return 0; }