• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compiling Ok but code has errors?

 
Juan Bonilla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21435
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juan Bonilla wrote:
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


Hint: what happens when you enter a blank line? If the line that is return is zero length, what happens when you ask for the char at position zero?

Henry
 
Juan Bonilla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats what is weird as soon as it ask me for the property price and i enter it for instance 45000 at that point exactly it wont let me enter anything else it makes an error happen
 
Henry Wong
author
Marshal
Pie
Posts: 21435
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Juan Bonilla wrote:thats what is weird as soon as it ask me for the property price and i enter it for instance 45000 at that point exactly it wont let me enter anything else it makes an error happen


It is caused by the previous read. A readDouble() only reads to a delimiter -- something that is not part of the double.

Since you entered a double number followed by a carriage return (as that is the only way to enter it from the keyboard), that CR is not part of the read. That CR is part of the next read. And since the next read is a read line, it reads a blank line.

Pretty common mistake. Add a check to make sure that you read a line that actually contains something.

Henry
 
Juan Bonilla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that was amazingly helpful it resolved my whole situation ! thanks a lot its fixed just a quick question could you give me a hint how can i make all the values to add up depending on how many the user enters and display a total for all of them ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a lot of confusion about the nextLine() method, which I wrote about here twenty minutes ago.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic