StringTokenizer help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: StringTokenizer help

  1. #1
    Join Date
    Nov 2003
    Posts
    1

    Question StringTokenizer help

    Hi folks,
    Could anyone please suggest ways to rewrite the addToppings method, in an even simpler way if possible. I'm new to java would like to see other ways to write the StringTokenizer used in this class.
    Thanks.
    Code:
    public class Pizza{
    String str = "prices.txt";
    BufferedReader br = null;
    StringTokenizer st = null;
    String strPizzaName;
    int arrayNum = 0;;
    int amount;
    float pizzaPrice;
    Toppings[] toppings;
    Base base;
    DecimalFormat df = new DecimalFormat("0.00");
    
    public Pizza(){
    pizzaPrice = 0;
    strPizzaName = "Please Select";
    base = new Base();
    amount = 0;
    toppings = new Toppings[20];
    addToppings();
    }
    
    public void addToppings(){
    //Search through "prices.txt" for price of Pizza Price & name
    try{
    br = new BufferedReader(new FileReader(str));
    }catch (FileNotFoundException nf) {}
    try {
    String temp = " ";
    String name = " ";
    String cost = " ";
    int tokNum = 0;
    while (temp != null) {
    //Change words into small strings called Tokens
    st = new StringTokenizer(temp);
    while (st.hasMoreTokens()) {
    tokNum++;
    String sTok = st.nextToken();
    if (tokNum == 1) {
    name = sTok;
    }
    if (tokNum == 2) {
    cost = sTok;
    }
    }
    temp = br.readLine();
    if (tokNum == 2) {
    float price = Float.parseFloat(cost);
    toppings[arrayNum] = new Toppings(name, price);
    arrayNum++;
    }
    tokNum = 0;
    }
    br.close();
    
    }catch (IOException ie){
    }catch (NoSuchElementException nse){}
    } 
    
    float getPrice() {
    //Return the Price per Pizza & amount
    pizzaPrice = 0;
    for (int i = 0; i < arrayNum; i++){
    pizzaPrice = pizzaPrice + toppings.getToppCost();
    }
    pizzaPrice = pizzaPrice + base.getPrice();
    pizzaPrice = pizzaPrice * amount;
    pizzaPrice = Float.parseFloat(df.format(pizzaPrice));
    return pizzaPrice;
    }
    
    int getArrayAmount(){
    return arrayNum;
    }
    
    public void setPizzaName(String name) {
    strPizzaName = name;
    }
    
    public String getPizzaName() {
    return strPizzaName;
    }
    
    public void setAmount(int pizzaAmount){
    amount = pizzaAmount;
    }
    
    public int getAmount(){
    return amount;
    }
    
    }
    [ArchAngel added CODE tags]

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    write the StringTokenizer used in this class
    Huh?
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Nov 2003
    Posts
    9
    I just simply can't read codes that are not idented. I tried reading your code but i got nauseous and dissoriented. But i can tell you an alternative way to parse out strings into tokens, and that's the new split method for strings. It's new in version 1.4 so if you have it, use it!!!

    To parse a string with a delimiter of " " you can just go
    "I am whatever".split(" ");
    which will return an array of string tokens.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center