Results 1 to 12 of 12
  1. #1
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Question [SOLVED] Expected errors, I can't see what's wrong.

    Java Code:
    public String toString()
            
        {
        return String.format("%s: %s\n %s: %.2f\n %s: %.2f\n %s: %.2f" 
                "Item name",getItemName(), 
                "Item Number",getItemNumber(),
                "Stock",getInvStock(),
                "Item Price",getItemPrice(), 
                "Total Inventory Value",getCalculateInventoryValue());
        }
    I keep getting errors that tell me ")" is expected and ';' is expected but I am not sure where they are supposed to go. This looks right to me (and the text book) so I am not too sure where I went wrong. The whole program is posted below. There are other issues I have to deal with too but getting this right is my first step I think. :confused:

    Java Code:
    public class Inventory
    {
        private String itemName; // variable that stores the cartridge name
        private int itemNumber; // variable that stores the item number
        private int invStock; // variable that stores the quantity in stock
        private double itemPrice; // variable that stores the cartridge price
        public double getValue;
        public String calculateInventoryValue;
    
    public Inventory(String name, int number, int stock, double price)
        {
        itemName = name;
        itemNumber = number;
        invStock = stock;
        itemPrice = price;
        }
    
    public void setItemName(String name) // Method to set the item name
            
        {
        this.itemName = name;
        }
    
    public String getItemName() // Method to get the item name
            
        {
        return itemName;
        } 
    public void setItemNumber(int number) // Method to set the item number
            
        {
        this.itemNumber = number;
        }
    
    public int getItemNumber() // Method to get the item number
            
        {
        return itemNumber;
        }
    
    public void setinvStock(int stock) // Method to set the stock in stock
            
        {
        invStock = stock;
        }
    
    public int getInvStock() // Method to get the stock in stock
            
        {
        return invStock;
        }
    
    public void setItemPrice(double price) // Method to set the item price
            
        {
        this.itemPrice = price;
        }
    
    public double getItemPrice() // Method to get the item price
            
        {
        return itemPrice;
        }
    
    public double getCalculateInventoryValue() // Method to calculate the value of the inventory
            
        {
        return (double) itemPrice * invStock;
        }
    
    public double getValue()
    {
    
    }
    public int compareTo(Object o)
    {
            Inventory p = null;
            try
     {
            p = (Inventory) o;
     }
    
        catch (ClassCastException cE)
     {
            cE.printStackTrace();
     }
    
        return itemName.compareTo(p.getItemName());
    }
    
    public String toString()
            
        {
        return String.format("%s: %s\n %s: %.2f\n %s: %.2f\n %s: %.2f" 
                "Item name",getItemName(), 
                "Item Number",getItemNumber(),
                "Stock",getInvStock(),
                "Item Price",getItemPrice(), 
                "Total Inventory Value",getCalculateInventoryValue());
        }
    
        private void calculateInventoryValue() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    } //end class Product
    
    class DVD extends Inventory
    {
    private double reStockingFee;
    
    public DVD(String itemName, int itemNumber, int invStock, double itemPrice, double reStockingFee) 
    {
    super(itemName, itemNumber, invStock, itemPrice);
    this.reStockingFee = reStockingFee;
    } 
    public double getItemPrice() //returns the value of the inventory, plus the restocking fee
    {
    return super.getItemPrice() + reStockingFee;
    }
    public String toString() 
    {
    return new StringBuffer().append("Price: " + super.getItemPrice()).append(" With RestockingFee: " + getItemPrice()).toString();
    }
    
    } // end class Product
    
    -------
    
    import java.util.Scanner;
    import java.util.Arrays; 
    
    public class Inventory3
    
    {
    public static void main(String args[] )
    {
    double restockFee = 0.05; 
    
    //Declare and initialize the inventory array
    Inventory[] inventory = new Inventory[5]; //Array variables
    
    inventory[0] = new Inventory("Robin Hood", 1001, 3, 10.95);
    inventory[1] = new Inventory("NCIS Season 4", 1002, 2, 44.00);
    inventory[2] = new Inventory("The Departed", 1003, 4, 22.45);
    inventory[3] = new Inventory("A Man Apart", 1004, 16, 18.99);
    inventory[4] = new Inventory("Gone with the Wind", 1005, 14, 13.50);
    
    Inventory temp[] = new Inventory[1];
    
    System.out.println("Inventory Program for DVD's"); //display header
    System.out.println(); // blank line
    System.out.println(); // blank line
    for(int i = 0; i < inventory.length; i++)
    {
    System.out.println("Item Number: " + inventory[i].getItemNumber());
    System.out.println("Item Name: " + inventory[i].getItemName());
    System.out.println("Inventory On Hand: " + inventory[i].getInvStock());
    System.out.printf("Item Price: $%,.2f\n " + inventory[i].getItemPrice());
    System.out.printf("Value of Inventory: $%,.2f\n " + inventory[i].getValue());
    
    System.out.println("Restock Fee: " + (inventory[i].getInvStock() * inventory[i].getItemPrice()) * 0.05);
    System.out.println(); 
    
    } 
    } 
    
    } //End class Inventory3

  2. #2
    theonly is offline Member
    Join Date
    Apr 2008
    Posts
    23
    Rep Power
    0

    Default

    I think your problem is in your concatenation instead of using "," in between "Item Name" use a "+" and so on.

  3. #3
    orchid's Avatar
    orchid is offline Member
    Join Date
    Apr 2007
    Location
    Midwest
    Posts
    60
    Rep Power
    0

    Default

    You are missing a comma here after this :
    "%s: %s\n %s: %.2f\n %s: %.2f\n %s: %.2f"
    see below:
    Java Code:
    public String toString()
            
        {
        return String.format("%s: %s\n %s: %.2f\n %s: %.2f\n %s: %.2f" ,
                "Item name",getItemName(), 
                "Item Number",getItemNumber(),
                "Stock",getInvStock(),
                "Item Price",getItemPrice(), 
                "Total Inventory Value",getCalculateInventoryValue());
        }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    As theonly says, use of basic string concatenation with + sign is much easier to work on with. Nothing to misses much things like you have done.

  5. #5
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    I took what Orchid says and it seems to get rid of the errors. With the + sign, it was telling me that it will not work with java.lang.string. I am pretty sure I need to use a wrapper for it but I cant seem to get it to work. Now with that minor change, I am getting <No main classes found> . This seems to be my number 1 error on every project I have. Can anyone see why it would say that? Both have a main class. Also on my Inventory 3, it is saying there is no main method! :O ARGH!

    (And thanks everyone for helping!)
    Last edited by Azzia; 05-22-2008 at 08:07 PM. Reason: Thanks

  6. #6
    Jman is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    Azzia

    I am not sure if this will work for you now. I got this code to compile by adding a comma in the following line

    return String.format("%s: %s\n %s: %.2f\n %s: %.2f\n %s: %.2f",<------ I added this comma in "Item name",getItemName(),
    "Item Number",getItemNumber(),
    "Stock",getInvStock(),
    "Item Price",getItemPrice(),
    "Total Inventory Value",getCalculateInventoryValue());

    here is the whole code that did compile

    public class Inventory
    {
    private String itemName; // variable that stores the cartridge name
    private int itemNumber; // variable that stores the item number
    private int invStock; // variable that stores the quantity in stock
    private double itemPrice; // variable that stores the cartridge price
    public double getValue;
    public String calculateInventoryValue;

    public Inventory(String name, int number, int stock, double price)
    {
    itemName = name;
    itemNumber = number;
    invStock = stock;
    itemPrice = price;
    }

    public void setItemName(String name) // Method to set the item name

    {
    this.itemName = name;
    }

    public String getItemName() // Method to get the item name

    {
    return itemName;
    }
    public void setItemNumber(int number) // Method to set the item number

    {
    this.itemNumber = number;
    }

    public int getItemNumber() // Method to get the item number

    {
    return itemNumber;
    }

    public void setinvStock(int stock) // Method to set the stock in stock

    {
    invStock = stock;
    }

    public int getInvStock() // Method to get the stock in stock

    {
    return invStock;
    }

    public void setItemPrice(double price) // Method to set the item price

    {
    this.itemPrice = price;
    }

    public double getItemPrice() // Method to get the item price

    {
    return itemPrice;
    }

    public double getCalculateInventoryValue() // Method to calculate the value of the inventory

    {
    return (double) itemPrice * invStock;
    }


    public int compareTo(Object o)
    {
    Inventory p = null;
    try
    {
    p = (Inventory) o;
    }

    catch (ClassCastException cE)
    {
    cE.printStackTrace();
    }

    return itemName.compareTo(p.getItemName());
    }

    public String toString()

    {
    return String.format("%s: %s\n %s: %.2f\n %s: %.2f\n %s: %.2f",//<------ I added this comma in
    "Item name",getItemName(),
    "Item Number",getItemNumber(),
    "Stock",getInvStock(),
    "Item Price",getItemPrice(),
    "Total Inventory Value",getCalculateInventoryValue());
    }

    private void calculateInventoryValue() {
    throw new UnsupportedOperationException("Not yet implemented");
    }
    } //end class Product

    class DVD extends Inventory
    {
    private double reStockingFee;

    public DVD(String itemName, int itemNumber, int invStock, double itemPrice, double reStockingFee)
    {
    super(itemName, itemNumber, invStock, itemPrice);
    this.reStockingFee = reStockingFee;
    }
    public double getItemPrice() //returns the value of the inventory, plus the restocking fee
    {
    return super.getItemPrice() + reStockingFee;
    }
    public String toString()
    {
    return new StringBuffer().append("Price: " + super.getItemPrice()).append(" With RestockingFee: " + getItemPrice()).toString();
    }

    } // end class Product

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    After doing that minor change everything working fine here, except one thing. On Inventory3 you made a mistake on following two lines. Try to identify and correct it.

    Java Code:
    System.out.printf("Item Price: $%,.2f\n " + inventory[i].getItemPrice());
    System.out.printf("Value of Inventory: $%,.2f\n " + inventory[i].getValue());

  8. #8
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    Java Code:
    System.out.printf("Item Price: $%,.2f\n " + inventory[i].getItemPrice());
    System.out.printf("Value of Inventory: $%,.2f\n " + inventory[i].getValue());
    Doesn't seem to be giving me errors. I do have a suggestion on how it could be wrong though. First thing that comes to mind is that I do not need the
    Java Code:
     $%,.2f\n
    in the code because it is already given in Inventory. I took it out and will try it. NetBeans just seems to hate me and keeps telling me that there are <No Main Classes Found> :confused:

    Thanks again for the help. I really do appriciate it.
    Last edited by Azzia; 05-23-2008 at 09:36 PM. Reason: Thanks again!

  9. #9
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    WoAH!

    That wasn't what was wrong, I think I about blew up my program ahaha! Let me look at it with more detail and ill get back with you. I fixed the no main class issue as well.

  10. #10
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    Ok, I took out the commas and that wasn't it either. They are both doubles and need since there is a format specifier, .printf is needed. The money sign comes before the format specifier so it is in the right order... trying
    Java Code:
    System.out.printf("Item Price: $%.2f\n" + inventory[i].getItemPrice());
    System.out.printf("Value of Inventory: $%.2f\n" + inventory[i].getValue());
    doesn't work either. I keep getting

    Java Code:
    Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '.2f' 
    
            at java.util.Formatter.format(Formatter.java:2431)
    
    Item Price: $
    
            at java.io.PrintStream.format(PrintStream.java:920)
            at java.io.PrintStream.printf(PrintStream.java:821)
            at inventory.Inventory3.main(Inventory3.java:39)
    I am terrible at reading errors too. Hmmm. Back to work.

  11. #11
    Azzia is offline Member
    Join Date
    May 2008
    Location
    Ohio
    Posts
    20
    Rep Power
    0

    Default

    Alrighty. I changed them all to:

    Java Code:
    {
                System.out.println("Item Number: " + inventory[i].getItemNumber());
                System.out.println("Item Name:  " + inventory[i].getItemName());
                System.out.println("Inventory On Hand: " + inventory[i].getInvStock());
                System.out.println("Item Price: " + inventory[i].getItemPrice());
                System.out.println("Value of Inventory: " + inventory[i].getValue());
    
                System.out.println("Restock Fee: " + (inventory[i].getInvStock() * inventory[i].getItemPrice()) * 0.05);
                System.out.println();

    and now I get:

    Java Code:
    Item Number: 1005
    Item Name:  Gone with the Wind
    Inventory On Hand: 14
    Item Price: 13.5
    Value of Inventory: 189.0
    Restock Fee: 9.450000000000001
    Where the restocking fee needs to be formated. Back to work again.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Cannot get passed these syntax errors
    By MrKP in forum New To Java
    Replies: 1
    Last Post: 05-12-2008, 08:05 AM
  2. help with these errors
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 05:55 PM
  3. Errors I don't understand
    By MattyB in forum New To Java
    Replies: 4
    Last Post: 04-02-2008, 12:55 AM
  4. I have 3 errors after compiling
    By coco in forum JDBC
    Replies: 2
    Last Post: 10-18-2007, 10:32 AM
  5. Errors in constructor
    By ai_2007 in forum Advanced Java
    Replies: 0
    Last Post: 07-01-2007, 06:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •