I want to thank everyone before hand for your help I'm a
Java Rookie in a problem with an Assingment it compiles no problem when i run it it says:
java.lang.StringIndexOutOfBoundsException:
String index out of range: 0
at java.lang.String.charAt(String.java:695)
at comissione.main(comissione.java:54)
the error the compiler says its because when i try to read a CHAR with Scanner but I'm not sure why it copiles fiiiiine at the beggining i Cant understand or figure out what am i doing wrong
im making char to be readable for scanner the problem is when i run it
code:
//These Statements Import The Necessary Libraries
import java.text.DecimalFormat;
import java.util.Scanner;
//The Class Is Public
public class comissione
{
//This Is The Main Method
public static void main (String [] args)
{
//Declaring Variables In This Block
String option; //Holding The Input
double comiComissione; //To Hold Comission Total
double salePrice; //Holding The Sales Price
char choice; //To Hold The Y or N
char propertyCode; //Holding The Property Code
//To Create A New Scanner Object
Scanner readable = new Scanner(System.in);
// Create a DecimalFormat Object.
DecimalFormat f1 = new DecimalFormat("#0.00");
//Program Information Is Displayed Here
System.out.print(" \t\tMy name \n ");
System.out.println(" \tReal State Comission Calculator \n");
//This Whole Portion Its On A DO-While Loop As A Post-test Loop It Allows For The Portion To Run As Long As The Answer Is Y
do
{
System.out.print("Hello Please Enter The Property's Selling Price: ");
salePrice = readable.nextDouble();
System.out.print("Also Please Enter The Property Code According To The Following Choices: \n");
System.out.println(" Residential\t\t Enter R ");
System.out.println(" Multi-Dwelling\t\t Enter M ");
System.out.println(" Commercial\t\t Enter C ");
option = readable.nextLine();
propertyCode = option.charAt(0);
switch(propertyCode)
{
case 'R':
calcCumishun(salePrice * 0.7);
break;
case 'B':
calcCumishun(salePrice * 0.6);
break;
case 'C':
calcCumishun(salePrice * 0.35);
break;
default:
System.out.println("Invalid choice, Code Must Be Either an R, M, Or a C!" );
}
//Asking The User If He Wishes To Enter More Comissions
System.out.println("Would you like to Enter " +
"More Comissions? ");
System.out.print("Enter Y for Yes or N for No: ");
option = readable.nextLine(); //Reads The Line
choice = option.charAt(0); //This Allows Us To Get The First Char.
} while (choice == 'Y' || choice == 'y');
}
//Creating The Required Method As Per Assignment GuideLines
/**This Method Takes Receives The Sales Price And Property
As Arguments And Returns The Calculated Comission*/
public static void calcCumishun(double comiComissione)
{
System.out.println("The Calculated Comission is " + comiComissione);
}
}
/code
according to blue J the error is exactly when i try to read the property code at:
option = readable.nextLine();
propertyCode = option.charAt(0);
i know scanner CANNOT read char but thats what im trying to do it compiles fine at the beginning but i Don't understand why it wont run