I get this terrible error "StringIndexOutOfBoundsException string " when I run my code which successfully compiled and wonder how to fix it:
import java.util.Scanner; // Scanner Class for keyboard input
import java.text.DecimalFormat; // Needed for DecimalFormat
public class assignment3Q1
{ //Start assignment3Q1 class
public static void main(String[] args)
{
//Declare constants & variables
int selling_price;
char property_code;
double commission,
total_property_sales,
total_commissions;
String input;
char more;
total_property_sales = 0.0;
total_commissions = 0.0;
// Create a Scanner object
Scanner keyboard = new Scanner(System.in);
// Print header
System.out.println("Deborah Michaliszyn \n" +
"\n\nReal Estate Commission Calculator");
do
{
//Get user's property selling price input.
System.out.print("Enter the property selling price: ");
selling_price = keyboard.nextInt();
// check if sales_price is a negative value
while (selling_price<0)
{
System.out.println("Enter : you must enter a positive value /n" +
"for selling price, Try again: ");
selling_price = keyboard.nextInt();
}
// Enter the property code
System.out.println(
"Enter the property code according to the following : \n" +
"Residential enter R \n " +
"Multi-Dwelling enter M \n " +
"Commercial enter C \n\n" +
"Choose your code: ");
property_code = keyboard.nextLine().trim().toUpperCase().charAt(0);
while(property_code != 'R' && property_code != 'M' && property_code != 'C' )
{
System.out.println("Code must be an R, M, or a C!\n\n Choose your code: ");
property_code = keyboard.nextLine().trim().toUpperCase().charAt(0);
}
commission= calcCommission(property_code, selling_price);
// Display the commission.
System.out.println("the commission is :" + commission);
total_property_sales += selling_price; // Adds up total property sales.
total_commissions += commission; // Adds up commissions.
//ask user if they want more commissions
System.out.print("More Commissions? Y/N :");
input = keyboard.nextLine(); //reads a line
more = input.charAt(0); //get the first character
}while (more == 'Y' || more == 'y');
// Display the results.
System.out.println("Total Property sales: " + total_property_sales + " Total commissions " +
total_commissions);
}//end main
// call calcCommision method with arguments
public static double calcCommission(char property_code, int selling_price)
{
final double RESIDENTIAL_RATE = 0.07,
MULTI_DWELLING_RATE = 0.06,
COMMERCIAL_RATE = 0.035;
double commission_rate= 0;
System.out.println("Choose your code : \n");
switch (property_code)
{
case 'R': commission_rate = RESIDENTIAL_RATE; break;
case 'M': commission_rate = MULTI_DWELLING_RATE ;break;
case 'C': commission_rate = COMMERCIAL_RATE;
}
//calculate commision rate
return selling_price * commission_rate;
}//end calcCommission
}