Results 1 to 8 of 8
- 04-01-2011, 03:41 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 12
- Rep Power
- 0
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.
- 04-01-2011, 03:42 AM #2
I've heard of this thing called an if statement.
- 04-01-2011, 03:44 AM #3
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Etc.Java Code:char c; if (c=='x') System.exit(0);
Last edited by Solarsonic; 04-01-2011 at 03:52 AM.
- 04-01-2011, 03:48 AM #4
[cough]
syntax
[/cough]
Stoopid minimal length
- 04-01-2011, 03:50 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 12
- Rep Power
- 0
Hah ok that may be simple enough to get me going thanks guys. I will be back. :)
- 04-01-2011, 03:51 AM #6
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
- 04-01-2011, 04:35 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 12
- Rep Power
- 0
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:
and for my static method 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; } }
Any tips and advice is appreciated. ThanksJava 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." }
- 04-01-2011, 04:41 AM #8
Invalid syntax.Java Code:if customerType = "x" {
Single equals sign is assignment
Do not compare objects (Strings are objects) with equals signs. Use the equals method instead.
What is customerType?
Cannot compare multiple values that way. if customerType equals ... or customerType equals ... or etc. (NOTE this is not correct syntax, research logical operators.)Java Code:else if customerType = "c,r,t"
Similar Threads
-
Help with random letters.
By jenxin in forum New To JavaReplies: 110Last Post: 02-28-2011, 03:32 PM -
What does the letters mean?
By mustachMan in forum New To JavaReplies: 3Last Post: 02-11-2010, 09:50 PM -
how do i print a specific txt file on a specific printer
By nikhilbhat in forum New To JavaReplies: 2Last Post: 11-08-2008, 10:40 AM -
Need help with counting letters
By mrdestroy in forum New To JavaReplies: 15Last Post: 10-22-2008, 01:33 PM -
Letter with Letters
By elgatoboricua in forum New To JavaReplies: 7Last Post: 09-16-2008, 02:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks