2 Replies - 753 Views - Last Post: 05 October 2012 - 07:47 PM Rate Topic: -----

#1 KnownAsker21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-October 12

String index out of range when using charAt()

Posted 05 October 2012 - 06:42 PM

I'm getting an error in my code.

It seems to be a problem with how I use the charAt(). The second time using it, it gives me a string index out of range for some reason.

Here is my main

[code]/*
* Filename: CoffeeOrder
* Created on 10/4/2012
*
* ttran2
* Lecture Section: 7
* Instructor: Matsuda
* Lab Section: 9
* Instructor: Leon
*/
/**
* This program acts with the CoffeeApp.java
* This has constant variables.
* It will take the types of coffees and amounts wanted and give back the price.
* also prints out the menu and the bill of the coffees.
*
*/
//Declares variables and constants.
public class CoffeeOrder {
final double TAXRATE = .075;
private final int COLUMBIAN = 25;
private final int KENYAN = 20;
private final int FRENCH = 18;
private final int BRAZIL = 16;
int numberOfCoffeeC;
int numberOfCoffeeK;
int numberOfCoffeeF;
int numberOfCoffeeB;
double subTotal ;
//sets the variables to zero
public CoffeeOrder()
{
numberOfCoffeeC =0;
numberOfCoffeeK =0;
numberOfCoffeeF =0;
numberOfCoffeeB =0;
subTotal =0;
}
/**
* Returns the constant variables for when they are called.
* @return
*/
public double getSub()
{
return subTotal;
}
public double getTax()
{
return TAXRATE;
}

public int getColumbian()
{
return COLUMBIAN;
}
public int getKenyan()
{
return KENYAN;
}
public int getFrench()
{
return FRENCH;
}
public int getBrazil()
{
return BRAZIL;
}

/**
*
* @param coffeeType
* coffeeType takes input from the user to determine what type of Coffee they want
* @param numberCoffee
* numberCoffee is input taken from the user to determine the amount of Coffee they want to buy
* @return
* returns the price of their purchase
*/
public int sellCoffee(char coffeeType, int numberCoffee)
{
int CoffeePrice = 0;
if(coffeeType == 'C')
{
numberOfCoffeeC+=numberCoffee;
CoffeePrice = COLUMBIAN*numberCoffee;
subTotal+=CoffeePrice;
System.out.println("Your current bill is: $" + subTotal);
return CoffeePrice;
}
else if(coffeeType == 'K')
{
numberOfCoffeeK+=numberCoffee;
CoffeePrice = KENYAN*numberCoffee;
subTotal+=CoffeePrice;
System.out.println("Your current bill is: $" + subTotal);
return CoffeePrice;
}
else if (coffeeType == 'F')
{
numberOfCoffeeF+=numberCoffee;
CoffeePrice = FRENCH*numberCoffee;
subTotal+=CoffeePrice;
System.out.println("Your current bill is: $" + subTotal);
return CoffeePrice;
}
else if (coffeeType == 'B')
{
numberOfCoffeeB+=numberCoffee;
CoffeePrice = BRAZIL*numberCoffee;
subTotal+=CoffeePrice;
System.out.println("Your current bill is: $" + subTotal);
return CoffeePrice;
}
else
{
return CoffeePrice;
}
}
/**
* Prints out the coffee sold and the price of it.
*/
public void printMenu()
{
System.out.println("Here is the Menu");
System.out.println("Our Columbian Roast coffee costs: $25");
System.out.println("Our Kenyan Coffee costs: $20");
System.out.println("Our French Roast Coffee costs: $18");
System.out.println("Our Brazilian Blend Coffee costs: $16");
}
/**
* Prints out the bill of the customer, formatted.
* @param frequent
* A flag to see whether they are a frequent purchaser
* @param taxR
* A flag to see whether they are tax exempt.
*/
public void printBill(boolean frequent, boolean taxR)
{
if(numberOfCoffeeC>0)
{
System.out.println("Columbian Coffee:"+numberOfCoffeeC+"\t$"+numberOfCoffeeC*25 );
}
if(numberOfCoffeeK>0)
{
System.out.println("Keyan Coffee:"+numberOfCoffeeK+"\t$"+numberOfCoffeeK*20);
}
if(numberOfCoffeeF>0)
{
System.out.println("French Coffee:"+numberOfCoffeeF+"\t$"+numberOfCoffeeF*18);
}
if(numberOfCoffeeB>0)
{
System.out.println("Brazilian Blend:"+numberOfCoffeeB+"\t$"+numberOfCoffeeB*16);
}
if(frequent == true)
{
if(taxR == true)
{
System.out.println("-------------------------------");
System.out.println("Your subtotal is: $"+subTotal);
System.out.println("Discount\t$"+(subTotal*.05));
System.out.print("Your bill is: $");
System.out.print(subTotal=subTotal-subTotal*.05);
}

else
{
System.out.println("-------------------------------");
System.out.println("Your subtotal is: $"+subTotal);
System.out.println("Discount\t$"+(subTotal*.05));
System.out.println("Sales Tax\t$"+(subTotal*TAXRATE));
System.out.print("Your Bill is: $");
System.out.print(subTotal= (subTotal + (TAXRATE*subTotal) - (subTotal*.05)));
}

}
if(frequent == false)
{
if(taxR == true)
{
System.out.println("-------------------------------");
System.out.print("Your bill is: $");
System.out.print(subTotal);
}
else
{
System.out.println("-------------------------------");
System.out.println("Your subtotal is: $"+subTotal);
System.out.println("Taxes \t$"+subTotal*TAXRATE);
System.out.print("Your bill is: $");
System.out.print(subTotal= (subTotal + (TAXRATE*subTotal)));
}
}


}

}
[code/]
the class
[code]/*
* Filename: CoffeeApp
* Created on 10/4/2012
*
* ttran2
* Lecture Section: 7
* Instructor: Matsuda
* Lab Section: 9
* Instructor: Leon
*/
/**
* This works with the CoffeeOrder class.
* Asks the user for various input and uses it as parameters for the methods found in CoffeeOrder
*Uses a loop to see if the customer wants to buy more Coffee.
*prints their formatted bill at the end.
*/
import java.util.Scanner;
public class Coffeetest {
public static void main (String[] args)
{

Scanner keyboard = new Scanner(System.in);
char coffeeType;
int numberOfCoffee;
boolean tax;
boolean frequent;
char answer;
char frequentChar;
char taxChar;
double subTotal;

CoffeeOrder customer1 = new CoffeeOrder();


customer1.printMenu();


System.out.println("Would you like to purchase some Coffee?Y/N");
answer = keyboard.nextLine().toUpperCase().charAt(0);

//This determines the parameters for what type of Coffee to use and how many for the sellCoffee method.
while(answer == 'Y')
{
customer1.printMenu();
System.out.println("What type of coffee would you like to purchase?(CKFB)");
coffeeType = keyboard.nextLine().toUpperCase().charAt(0);
System.out.println("How many of that coffee would you like?");
numberOfCoffee = keyboard.nextInt();
customer1.sellCoffee(coffeeType,numberOfCoffee);
System.out.println("Would you like to buy more Coffee?Y/N");
answer = keyboard.nextLine().toUpperCase().charAt(0);
}

//This is used in an if statement later
subTotal=customer1.getSub();


/**
* Here we see their bill with discounts and tax (if they have discounts/tax)
* We also check to make sure they have bought stuff.
*/
if(subTotal>0)
{
System.out.println("Are you a frequent purchaser?Y/N");
frequentChar='N';
frequentChar=keyboard.nextLine().toUpperCase().charAt(0);
if(frequentChar=='Y')
{
frequent=true;
}
else
{
frequent = false;
}

System.out.println("Are you exempt from taxes?Y/N");
taxChar=keyboard.nextLine().toUpperCase().charAt(0);
if(taxChar=='N')
{
tax=false;
}
else
{
tax=true;
}
customer1.printBill(frequent, tax);
}
else
{
System.out.println("Have a nice day! Come back soon!");
}
}
}
[code/]

Is This A Good Question/Topic? 0
  • +

Replies To: String index out of range when using charAt()

#2 KnownAsker21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-October 12

Re: String index out of range when using charAt()

Posted 05 October 2012 - 06:54 PM

I'm getting an error in my code.

It seems to be a problem with how I use the charAt(). The second time using it, it gives me a string index out of range for some reason.

Here is my main

/*
* Filename: CoffeeOrder
* Created on 10/4/2012
*
* ttran2
* Lecture Section: 7
* Instructor: Matsuda
* Lab Section: 9
* Instructor: Leon
*/
/**
* This program acts with the CoffeeApp.java
* This has constant variables.
* It will take the types of coffees and amounts wanted and give back the price.
* also prints out the menu and the bill of the coffees.
*
*/
//Declares variables and constants.
public class CoffeeOrder {
final double TAXRATE = .075;
private final int COLUMBIAN = 25;
private final int KENYAN = 20;
private final int FRENCH = 18;
private final int BRAZIL = 16;
int numberOfCoffeeC;
int numberOfCoffeeK;
int numberOfCoffeeF;
int numberOfCoffeeB;
double subTotal ;
//sets the variables to zero
public CoffeeOrder()
{
numberOfCoffeeC =0;
numberOfCoffeeK =0;
numberOfCoffeeF =0;
numberOfCoffeeB =0;
subTotal =0;
}
/**
* Returns the constant variables for when they are called.
* @return
*/
public double getSub()
{
return subTotal;
}
public double getTax()
{
return TAXRATE;
}

public int getColumbian()
{
return COLUMBIAN;
}
public int getKenyan()
{
return KENYAN;
}
public int getFrench()
{
return FRENCH;
}
public int getBrazil()
{
return BRAZIL;
}

/**
* 
* @param coffeeType
* coffeeType takes input from the user to determine what type of Coffee they want
* @param numberCoffee
* numberCoffee is input taken from the user to determine the amount of Coffee they want to buy
* @return
* returns the price of their purchase
*/
public int sellCoffee(char coffeeType, int numberCoffee)
{
int CoffeePrice = 0;
if(coffeeType == 'C')
{
numberOfCoffeeC+=numberCoffee;
CoffeePrice = COLUMBIAN*numberCoffee;
subTotal+=CoffeePrice;
System.out.println("Your current bill is: $" + subTotal);
return CoffeePrice;
}
else if(coffeeType == 'K')
{	
numberOfCoffeeK+=numberCoffee;
CoffeePrice = KENYAN*numberCoffee;
subTotal+=CoffeePrice;
System.out.println("Your current bill is: $" + subTotal);
return CoffeePrice;
}
else if (coffeeType == 'F')
{	
numberOfCoffeeF+=numberCoffee;
CoffeePrice = FRENCH*numberCoffee;
subTotal+=CoffeePrice;
System.out.println("Your current bill is: $" + subTotal);
return CoffeePrice;
}
else if (coffeeType == 'B')
{
numberOfCoffeeB+=numberCoffee;
CoffeePrice = BRAZIL*numberCoffee;
subTotal+=CoffeePrice;
System.out.println("Your current bill is: $" + subTotal);
return CoffeePrice;
}
else
{
return CoffeePrice;
}
}
/**
* Prints out the coffee sold and the price of it.
*/
public void printMenu()
{
System.out.println("Here is the Menu");
System.out.println("Our Columbian Roast coffee costs: $25");
System.out.println("Our Kenyan Coffee costs: $20");
System.out.println("Our French Roast Coffee costs: $18");
System.out.println("Our Brazilian Blend Coffee costs: $16");
}
/**
* Prints out the bill of the customer, formatted.
* @param frequent
* A flag to see whether they are a frequent purchaser
* @param taxR
* A flag to see whether they are tax exempt.
*/
public void printBill(boolean frequent, boolean taxR)
{
if(numberOfCoffeeC>0)
{
System.out.println("Columbian Coffee:"+numberOfCoffeeC+"\t$"+numberOfCoffeeC*25 );
}
if(numberOfCoffeeK>0)
{
System.out.println("Keyan Coffee:"+numberOfCoffeeK+"\t$"+numberOfCoffeeK*20);
}
if(numberOfCoffeeF>0)
{
System.out.println("French Coffee:"+numberOfCoffeeF+"\t$"+numberOfCoffeeF*18);
}
if(numberOfCoffeeB>0)
{
System.out.println("Brazilian Blend:"+numberOfCoffeeB+"\t$"+numberOfCoffeeB*16);
}
if(frequent == true)
{
if(taxR == true)
{
System.out.println("-------------------------------");
System.out.println("Your subtotal is: $"+subTotal);
System.out.println("Discount\t$"+(subTotal*.05));	
System.out.print("Your bill is: $");
System.out.print(subTotal=subTotal-subTotal*.05);
}

else
{	
System.out.println("-------------------------------");
System.out.println("Your subtotal is: $"+subTotal);
System.out.println("Discount\t$"+(subTotal*.05));
System.out.println("Sales Tax\t$"+(subTotal*TAXRATE));
System.out.print("Your Bill is: $");
System.out.print(subTotal= (subTotal + (TAXRATE*subTotal) - (subTotal*.05)));
}

}
if(frequent == false)
{
if(taxR == true)
{
System.out.println("-------------------------------");
System.out.print("Your bill is: $");
System.out.print(subTotal);
}
else
{
System.out.println("-------------------------------");
System.out.println("Your subtotal is: $"+subTotal);
System.out.println("Taxes \t$"+subTotal*TAXRATE);
System.out.print("Your bill is: $");
System.out.print(subTotal= (subTotal + (TAXRATE*subTotal)));
}
}


}

}


the class
/*
* Filename: CoffeeApp
* Created on 10/4/2012
*
* ttran2
* Lecture Section: 7
* Instructor: Matsuda
* Lab Section: 9
* Instructor: Leon
*/
/**
* This works with the CoffeeOrder class.
* Asks the user for various input and uses it as parameters for the methods found in CoffeeOrder
*Uses a loop to see if the customer wants to buy more Coffee.
*prints their formatted bill at the end.
*/
import java.util.Scanner;
public class Coffeetest {
public static void main (String[] args)
{

Scanner keyboard = new Scanner(System.in);
char coffeeType;
int numberOfCoffee;
boolean tax;
boolean frequent;
char answer;
char frequentChar;
char taxChar;
double subTotal;

CoffeeOrder customer1 = new CoffeeOrder();


customer1.printMenu();


System.out.println("Would you like to purchase some Coffee?Y/N");
answer = keyboard.nextLine().toUpperCase().charAt(0);

//This determines the parameters for what type of Coffee to use and how many for the sellCoffee method.
while(answer == 'Y')
{
customer1.printMenu();
System.out.println("What type of coffee would you like to purchase?(CKFB)");
coffeeType = keyboard.nextLine().toUpperCase().charAt(0);
System.out.println("How many of that coffee would you like?");
numberOfCoffee = keyboard.nextInt();
customer1.sellCoffee(coffeeType,numberOfCoffee);
System.out.println("Would you like to buy more Coffee?Y/N");
answer = keyboard.nextLine().toUpperCase().charAt(0);	
}

//This is used in an if statement later
subTotal=customer1.getSub();


/**
* Here we see their bill with discounts and tax (if they have discounts/tax)
* We also check to make sure they have bought stuff.
*/
if(subTotal>0)
{
System.out.println("Are you a frequent purchaser?Y/N");
frequentChar='N';
frequentChar=keyboard.nextLine().toUpperCase().charAt(0);
if(frequentChar=='Y')
{
frequent=true;
}
else
{
frequent = false;
}

System.out.println("Are you exempt from taxes?Y/N");
taxChar=keyboard.nextLine().toUpperCase().charAt(0);
if(taxChar=='N')
{
tax=false;
}
else
{
tax=true;
}
customer1.printBill(frequent, tax);
}
else
{
System.out.println("Have a nice day! Come back soon!");
}
}
}


Was This Post Helpful? 0
  • +
  • -

#3 KnownAsker21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-October 12

Re: String index out of range when using charAt()

Posted 05 October 2012 - 07:47 PM

Got it solved.

Turns out I had to use next() instead of nextLine()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1