Re: Code Validation help??
Quote:
Originally Posted by
kmh90210
I am in a beginner Java class and we are supposed to validate the user input to only accept customer type r, c, or t. My code however, throws the error message with every entry, even valid ones. Can someone help me know what i'm doing wrong? Thanks in advance!
Code :
import java.text.NumberFormat;
import java.util.*;
public class ValidatedInvoiceApp
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
{
// get the input from the user
double subtotal = 0.0;
String customerType = "";
System.out.print("Enter customer type (r/c/t): ");
if (sc.hasNext())
{
if (customerType.equalsIgnoreCase ("r") || customerType.equalsIgnoreCase ("c") || customerType.equalsIgnoreCase ("t"))
{
customerType = sc.next();
}
else
{
sc.nextLine();
System.out.println("Error! Invalid Customer Type. Please Try Again. \n");
continue;
}
}
else
{
System.out.print("Enter subtotal: ");
subtotal = sc.nextDouble();
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)
{
// get the discount percent
double discountPercent = 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
discountPercent = .30;
}
else if (customerType.equalsIgnoreCase("C"))
{
discountPercent = .20;
}
else if (customerType.equalsIgnoreCase("T"))
{
if (subtotal < 500)
discountPercent = .40;
else if (subtotal >= 500)
discountPercent = .50;
}
return discountPercent;
}
}
Shouldn't Scanner be static? main is static. Also, what type of error message?
Also, if customer type is "" when it reaches the if loop, since you don't change it unless it's equal to those values, won't it always go to the else every time?
Re: Code Validation help??
When I run the code regardless of what customer type I enter it displays the error message in my loop. I can't get it to where it goes to the outside loop asking for the subtotal amount.
Re: Code Validation help??
Code :
boolean isAValidType = false;
while (isAValidType == false)
{
System.out.print("Enter customer type (r/c/t): ");
customerType = sc.next();
if (customerType.equalsIgnoreCase ("r") || customerType.equalsIgnoreCase ("c") || customerType.equalsIgnoreCase ("t"))
{
isAValidType = true;
}
else
{
sc.nextLine(); // what's this do?
customerType = ""; // resets customer type
System.out.println("Error! Invalid Customer Type. Please Try Again. \n");
//continue;
isAValidType = false
}
} // end while
Re: Code Validation help??
Quote:
throws the error message
What is the error message?
Can you run the program in a console window and copy and paste the contents of the window here?
Please edit your code and add code tags to preserver the formatting. Info here: Java Forums - BB Code List
Re: Code Validation help??
Enter customer type (r/c/t): r
Error! Invalid Customer Type. Please Try Again.
Norm,
Below is what the console prints out...the error message that I referred to was the error message I had created when invalid data was entered.
Enter customer type (r/c/t): c
Error! Invalid Customer Type. Please Try Again.
Enter customer type (r/c/t): t
Error! Invalid Customer Type. Please Try Again.
Enter customer type (r/c/t):
Re: Code Validation help??
Try debugging your code by adding print outs of the values that are being tested in the if statement
Also print out the value of the choice variable that controls the loop.
Also edit your code and add code tags. It is hard to read.
Re: Code Validation help??
Your problem is that you don't put anything in the String customerType. Just add the commented line.
Code java:
import java.text.NumberFormat;
import java.util.*;
public class ValidatedInvoiceApp
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
{
// get the input from the user
double subtotal = 0.0;
String customerType = "";
System.out.print("Enter customer type (r/c/t): ");
////////////////////customerTyper = sc.nextLine();
if (sc.hasNext())
{
if (customerType.equalsIgnoreCase ("r") || customerType.equalsIgnoreCase ("c") || customerType.equalsIgnoreCase ("t"))
{
customerType = sc.next();
}
else
{
sc.nextLine();
System.out.println("Error! Invalid Customer Type. Please Try Again. \n");
continue;
}
}
else
{
System.out.print("Enter subtotal: ");
subtotal = sc.nextDouble();
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)
{
// get the discount percent
double discountPercent = 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
discountPercent = .30;
}
else if (customerType.equalsIgnoreCase("C"))
{
discountPercent = .20;
}
else if (customerType.equalsIgnoreCase("T"))
{
if (subtotal < 500)
discountPercent = .40;
else if (subtotal >= 500)
discountPercent = .50;
}
return discountPercent;
}
}