I just finished writing this program for my java class. It's a simple program that calculates how much it costs to install a fence. At the end, it asks "would you like to make another estimate (Y/N)", and the user is suppose to choose yes or no. However, after I'm done running mine, I won't get that option, it just says:
Calculate Another Estimate (Y/N): Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:558)
at Test1.main(Test1.java:73)
Why is it doing this!!!
Here is the program by the way:
Calculate Another Estimate (Y/N): Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:558)
at Test1.main(Test1.java:73)
Why is it doing this!!!
Here is the program by the way:
Code:
public class Test1
{
public static void main( String[] args )
{
String customer = " ";
String customerUC = " ";
String type1 = " ";
String type2 = " ";
char type = ' ';
String another1 = " ";
char another = 'y';
int fence = 0;
int count = 0;
double costFoot = 0;
double costTotal = 0;
double costGrand = 0;
Scanner read = new Scanner(System.in);
System.out.print( "\n\t----------SIERRA FENCING COMPANY----------" );
while ( Character.toUpperCase(another) == 'Y' )
{
System.out.print( "\n\nYour Name: " );
customer = read.nextLine();
customerUC = customer.toUpperCase();
System.out.print( "\nNew Installation (N) or Repair (R): " );
type1 = read.nextLine();
type2 = type1.toUpperCase();
type = type2.charAt(0);
System.out.print( "\nAmount of Fencing Needed (linear feet): " );
fence = read.nextInt();
System.out.print( "\nCost-Per-Foot of Fencing: " );
costFoot = read.nextDouble();
if ( costFoot > 6 && type == 'N' )
{
costFoot = 6;
}
else if ( costFoot > 5 && type == 'R' )
{
costFoot = 5;
}
costTotal = fence * costFoot;
costGrand += costTotal;
count++;
System.out.print( "\n\n-------------------------------------------------------" );
System.out.print( "\n\tName: " + customerUC );
System.out.print( "\n\tType: " + type );
System.out.print( "\n\tLinear Fencing: " + fence + " feet" );
System.out.print( "\n\tCost-Per-Foot: $" + costFoot );
System.out.print( "\n\tTotal: $" + costTotal );
System.out.print( "\n-------------------------------------------------------" );
System.out.print( "\n\nCalculate Another Estimate (Y/N): " );
another1 = read.nextLine();
another = another1.charAt(0);
}
System.out.print( "\n\nGrand Total: $" + costGrand );
System.out.print( "\nYou made " + count + " estimates" );
}
}