Results 1 to 8 of 8
  1. #1
    ctmarco3 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default How to allow specific letters only?

    Hi, I am looking for a simple way to require an entry of either "a" "b" or "c"
    Or an "x" (to exit)
    If they enter anything else it should throw up a simple error "Example Error Invalid entry. Try again"
    discard any possible extra entries if any are present and prompt for reentry
    It needs to be a static method that I can reuse anytime.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    I've heard of this thing called an if statement.

  3. #3
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Java Code:
    char c;
    if (c=='x')
    System.exit(0);
    Etc.
    Last edited by Solarsonic; 04-01-2011 at 03:52 AM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    [cough]
    syntax
    [/cough]

    Stoopid minimal length

  5. #5
    ctmarco3 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Hah ok that may be simple enough to get me going thanks guys. I will be back. :)

  6. #6
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    [cough]
    syntax
    [/cough]

    Stoopid minimal length
    My bad, I wrote that without looking :p

  7. #7
    ctmarco3 is offline Member
    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    Default

    Ok maybe not, I do kind of understand if else statements for numbers, I guess I don't quite for letters though. Here is my (rough) code so far. I want to change the y/n input so instead it just runs over and over until an "x" is entered. Do I take the while statement out altogether? and for my "getValidCustomerType" method I am not even sure how to start it.
    main code:
    Java Code:
    import java.text.*;
    import java.util.*;
    
    public class ValidatedInvoiceApp		{
        public static void main(String[] args)	    {
    		//call method
    		 		MarcoHeading.getHeading("Assignment 4");
    
    		         // display a welcome message
             System.out.println("  Welcome to the Invoice Application\n");
            Scanner sc = new Scanner(System.in);
            String choice = "y";
    		//not sure how to start this part?
            while (choice.equalsIgnoreCase("y"))	{
            	
            	// get the input from the user
    			
    			//call method to validate customer type 
            	String customerType = getValidCustomerType(sc,
            		"  Enter customer type <C/R/T> or X to Exit:  ");
            		//call method to valdiate invoice amount
            	double discountAmount = getValidSubtotal(sc,
            	"		      Enter Invoice Amount:  ",0, 10000);
            	double subtotal = sc.nextDouble();
    
            	// call the method for getting the discount percent
            	double discountPercent = getDiscountPercent(customerType, subtotal);
    
            	// calculate the discount amount and total
            	double discountAmount = subtotal * discountPercent;
            	double total = subtotal - discountAmount;
    
            	// format and display the results
            	NumberFormat currency = NumberFormat.getCurrencyInstance();
            	NumberFormat percent = NumberFormat.getPercentInstance();
            	System.out.println(
            	    "Discount percent: " + percent.format(discountPercent) + "\n" +
            	    "Discount amount:  " + currency.format(discountAmount) + "\n" +
            	    "Total:            " + currency.format(total) + "\n");
    
            	// see if the user wants to continue
    			System.out.print("Continue? (y/n): ");
    			choice = sc.next();
                System.out.println();
    		}
    	}
    	private static double getDiscountPercent(String customerType, double subtotal)
    	{
    		double discountPercent = 0.0;
    		if (customerType.equalsIgnoreCase("R"))	{
    		    if (subtotal < 100)
    		        discountPercent = 0;
    		    else if (subtotal >= 100 && subtotal < 250)
    		        discountPercent = .1;
    		    else if (subtotal >= 250 && subtotal < 500)
    		        discountPercent = .25;
    		    else if (subtotal >= 500)
    		    	discountPercent = .3;
    		}
    		else if (customerType.equalsIgnoreCase("C"))
    		{	 discountPercent = .2;		}
    		else if (customerType.equalsIgnoreCase("T"))	{
    			if (subtotal < 500)
    				discountPercent = .4;
    			else if (subtotal >= 500)
    			    discountPercent = .5;
    				}
    		return discountPercent;
    	}
    }
    and for my static method code:
    Java Code:
    public static double getValidCustomerType(Scanner sc, String prompt)
    {
    	//is this right?
    	boolean isValid = false;
    	while (isValid ==false);
    	{
    		System.out.print(prompt);
    		//...?	
    	if customerType = "x" {
    		system.out.print("  Program terminated by the user\n")
    		//not sure how to code this?
    		else if customerType = "c,r,t"
    		{
    			return;
    		}
    		else system.out.print("  *Error* Invalid customer type. Try again."
    	
    }
    Any tips and advice is appreciated. Thanks

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    Java Code:
    if customerType = "x" {
    Invalid syntax.
    Single equals sign is assignment
    Do not compare objects (Strings are objects) with equals signs. Use the equals method instead.
    What is customerType?
    Java Code:
    else if customerType = "c,r,t"
    Cannot compare multiple values that way. if customerType equals ... or customerType equals ... or etc. (NOTE this is not correct syntax, research logical operators.)

Similar Threads

  1. Help with random letters.
    By jenxin in forum New To Java
    Replies: 110
    Last Post: 02-28-2011, 03:32 PM
  2. What does the letters mean?
    By mustachMan in forum New To Java
    Replies: 3
    Last Post: 02-11-2010, 09:50 PM
  3. Replies: 2
    Last Post: 11-08-2008, 10:40 AM
  4. Need help with counting letters
    By mrdestroy in forum New To Java
    Replies: 15
    Last Post: 10-22-2008, 01:33 PM
  5. Letter with Letters
    By elgatoboricua in forum New To Java
    Replies: 7
    Last Post: 09-16-2008, 02:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •