March 2nd, 2013, 07:09 AM
-
Delete thread
EDIT: Nevermind, I figured the rest out myself. Thanks for the help!
March 2nd, 2013, 09:54 AM
-
Please edit you post and change the quote tags to code tags.
March 2nd, 2013, 05:26 PM
-
Ok, I've updated the OP. Hope it makes more sense now!
March 2nd, 2013, 05:34 PM
-
I want extra spacing for this:
Add a \n at the end of the String printed by println() to get an extra newline characer printed which will cause a blank line to be printed.
March 2nd, 2013, 05:40 PM
-
Originally Posted by NormR
Add a \n at the end of the String printed by println() to get an extra newline characer printed which will cause a blank line to be printed.
haha, can't believe I forgot that. I feel stupid now. So, do you know how to solve my first problem to get rid of the extra "Enter subtotal: Error! Invalid number. Try again." that shows up when I enter an invalid subtotal like a letter?
March 2nd, 2013, 06:12 PM
-
Problem here:
Enter customer type (r/c): r
Enter subtotal: j
Error! Invalid number. Try again.
Enter subtotal: Error! Invalid number. Try again.
Enter subtotal:
Code:
import java.text.NumberFormat;
import java.util.Scanner;
import java.util.*;
public class InvoiceApp
{
public static void main(String[] args)
{
Scanner sc = new Scanner("r j\n"); //System.in);
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
if(choice.equalsIgnoreCase("y"))
{
// get the input from the user
String customerType = getValidCustomerType(sc);
double subtotal = 0.0;
// get the discount percent
double discountPercent = 0.0;
if (customerType.equalsIgnoreCase ("r") || customerType.equalsIgnoreCase ("c"))
{
if (customerType.equalsIgnoreCase("R"))
{
try
{
subtotal = getValidSubtotal(sc);
}
catch (InputMismatchException e)
{
sc.next();
System.out.println("1Error! Invalid number. Try again.\n");
continue;
}
if (subtotal < 100)
discountPercent = 0;
else if (subtotal >= 100 && subtotal < 250)
discountPercent = .1;
else if (subtotal >= 250)
discountPercent = .2;
}
else if (customerType.equalsIgnoreCase("C"))
{
try
{
subtotal = getValidSubtotal(sc);
}
catch (InputMismatchException e)
{
sc.next();
System.out.println("2Error! Invalid number. Try again.\n");
continue;
}
if (subtotal < 250)
discountPercent = .2;
else
discountPercent = .3;
}
}
else if (customerType != null)
{
sc.nextLine();
System.out.println("Error! Invalid Customer Type. Please enter either 'r' or 'c' to continue.\n");
continue;
}
// 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();
sc.nextLine();
System.out.println();
}
}
public static String getValidCustomerType (Scanner sc)
{
// get the input from the user
String customerType = "";
boolean isValid = false;
while (isValid == false)
{
System.out.print("Enter customer type (r/c): ");
if (sc.hasNext())
{
customerType = sc.next();
System.out.println(customerType); //<<<<<<<<<<<<
isValid = true;
}
else
{
sc.nextLine();
System.out.println("Error! Invalid Customer Type. Please enter either 'r' or 'c' to continue.\n");
}
}
return customerType;
}
public static double getValidSubtotal (Scanner sc)
{
double subtotal = 0.0;
boolean isValid = false;
while (isValid == false)
{
//get a valid double value
System.out.print("Enter subtotal: ");
if (sc.hasNextDouble())
{
subtotal = sc.nextDouble();
isValid = true;
}
else
{
String str = sc.nextLine();
System.out.println(str); //<<<<<<<<<<<<<
System.out.println("3Error! Invalid number:"+str+" Try again.\n");
}
if (isValid == true && subtotal <= 0)
{
System.out.println("Error! Number must be greater than 0. Try entering a greater number.");
isValid = false;
}
else if (subtotal >= 10000)
{
System.out.println("Error! Number must be less than 10000. Try entering a number with a lower value.");
isValid = false;
}
} // end while()
return subtotal;
}
}
I see that same message is printed in three places. Which one is being printed? Or are two different ones printing? For debugging add some letter to the messages to make them all unique. Also include the invalid value in the message that is printed.
Last edited by NormR; March 3rd, 2013 at 05:38 AM.
March 2nd, 2013, 09:23 PM
-
Originally Posted by NormR
I see that same message is printed in three places. Which one is being printed? Or are two different ones printing? For debugging add some letter to the messages to make them all unique. Also include the invalid value in the message that is printed.
I messed with it a little and it's still acting a little funny. I notice whenever I put "r +SPACE q" (I do this to see if my code ignores the second entry) that the extra line that says "Enter subtotal: Error! Invalid number. Try again." shows up and it also shows up when I put a random letter in the subtotal entry, but only after I first put an entry in for "Enter customer type (r/c)". Is this a glitch or something? (Sorry, if that didn't make sense).
March 2nd, 2013, 11:09 PM
-
I found a way around the last problem, but now I'm wondering what do I have to do to make that one error message display. More info in the OP which I edited!