Best Way for Yes or No Input
For class, I am working on a program that asks for input for speed limit, speed recorded, and if they were in a school zone or not for a ticket pricing. I wasn't sure on the best way to determine if the violator was in a school zone or not so I went with a boolean. Of course, this only works with 'true' or 'false' inputs. How would I change into to accept all forms of yes (caps, not caps, and some of both and y) and no (caps, not caps, and some of both as well as n)?
I'm not sure how to do this at all. Here is my current code, which works only with an input of yes or no for the school zone:
public class TextLab03st
public static void main(String args)
Scanner read = new Scanner(System.in); //creates a new scanner called read, used later for keyboard input.
System.out.println("What is the posted speed limit? --> "); //provides the user with a prompt to enter a double value for the speed limit in the area.
double spd_lmt = read.nextDouble(); //enter a decimal number to represent the speed limit in the area.
System.out.println("How fast was the car traveling in mph? --> "); //provides the user with a prompt to enter a double value for the speed of the car.
double car_spd = read.nextDouble(); //enter a decimal number to represent speed of the car.
System.out.println("Did the violation occur in a school zone (Y/N)?"); //provides the user with a prompt to enter a Y or N answer.
boolean Szone = read.nextBoolean();
double tkt_amt = 75; //defines a variable representing ticket amount to be paid; base value of 75 for all tickets.
tkt_amt += (car_spd - spd_lmt)*6; //6 dollars for every mph over the speed limit is added to the original $75.00
if(car_spd>(spd_lmt+30)) //if the car speed is more than 30 miles over the speed limit execute the following statement.
tkt_amt += 160; //add 160 to the ticket amount.
if(Szone) //if the driver was in a school zone.
tkt_amt *= 2; //multiply ticket amount by 2.
System.out.println("Ticket Amount: " + tkt_amt); //total ticket amount
Re: Best Way for Yes or No Input
Did you post the code you are talking about? I don't see where any code tests for any versions of "yes".
How would I change into to accept all forms of yes
One useful String class method ignores case when comparing Strings for equality.
See the API doc for how to use it.