Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Best Way for Yes or No Input

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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:


     
    import java.util.Scanner;
     
    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
    		System.out.println();
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Best Way for Yes or No Input

    How would I change into to accept all forms of yes
    Did you post the code you are talking about? I don't see where any code tests for any versions of "yes".

    One useful String class method ignores case when comparing Strings for equality.
    See the API doc for how to use it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Input/Output help if possible
    By rossonomous in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 13th, 2011, 01:05 PM
  2. Input
    By DeFactos in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 18th, 2011, 04:05 AM
  3. Getting input
    By BuhRock in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2010, 10:30 AM
  4. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM
  5. Values of Input
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 8th, 2009, 03:46 AM