Reputation: 13
I am trying to write a ' try and catch ' method in this example, but for some reason I'm getting in error on all of my variables saying; "cannot find symbol". This is happening in all instances of: subtotal & customerType. Does anyone know what can be causing this?
import java.text.NumberFormat;
import java.util.Scanner;
import java.util.*;
public class InvoiceApp
{
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
try
{
//System.out.print("Enter customer type (r/c): ");
String customerType = getValidCustomerType(sc);
System.out.print("Enter subtotal: ");
double subtotal = sc.nextDouble();
}
catch (InputMismatchException e)
{
sc.next();
System.out.println("Error! Invalid number. Try again \n");
continue;
}
// 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)
discountPercent = .2;
}
else if (customerType.equalsIgnoreCase("C"))
{
if (subtotal < 250)
discountPercent = .2;
else
discountPercent = .3;
}
else
{
discountPercent = .1;
}
// 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();
}
}
Upvotes: 1
Views: 158
Reputation: 13
private static String getValidCustomerType(Scanner sc)
{
String customerType = "";
boolean isValid = false;
while (isValid == false)
{
System.out.print("Enter customer type (r/c): ");
customerType = sc.next();
if (customerType.equalsIgnoreCase("r") || (customerType.equalsIgnoreCase("c")))
isValid = true;
else
{
System.out.println("Invalid customer type. Try again. \n");
}
sc.nextLine();
}
return customerType;
}
}
Upvotes: 0
Reputation: 435
You declared subtotal and customertype inside the brackets of the try {}. That is the only place where they are valid. If you declared them before the try this would have been fine.
Upvotes: 0
Reputation: 726479
The problem with your code is the scope of your variables: if you declare something in a try
block, it is visible only within that try
block; it is not going to be visible outside the try
block, including even the catch
blocks after it.
In order to get around this problem, declare the variable outside the try
block:
String customerType;
double subtotal;
try {
//System.out.print("Enter customer type (r/c): ");
customerType = getValidCustomerType(sc);
System.out.print("Enter subtotal: ");
subtotal = sc.nextDouble();
} catch (InputMismatchException e) {
sc.next();
System.out.println("Error! Invalid number. Try again \n");
continue;
}
Upvotes: 1
Reputation: 18569
You declare subtotal
and customerType
in try block, so that variable only visible in try block.
Change your code like this will fix the problem:
double subtotal = 0;
String customerType = "";
try
{
//System.out.print("Enter customer type (r/c): ");
String customerType = getValidCustomerType(sc);
System.out.print("Enter subtotal: ");
subtotal = sc.nextDouble();
}
catch (InputMismatchException e)
{
sc.next();
System.out.println("Error! Invalid number. Try again \n");
continue;
}
More : Blocks and Statements
Upvotes: 1