Results 1 to 12 of 12
- 05-21-2008, 07:42 PM #1
Member
- Join Date
- May 2008
- Location
- Ohio
- Posts
- 20
- Rep Power
- 0
[SOLVED] Expected errors, I can't see what's wrong.
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 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()); }
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
- 05-21-2008, 11:16 PM #2
Member
- Join Date
- Apr 2008
- Posts
- 23
- Rep Power
- 0
I think your problem is in your concatenation instead of using "," in between "Item Name" use a "+" and so on.
- 05-21-2008, 11:34 PM #3
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()); }
- 05-22-2008, 05:28 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 05-22-2008, 07:06 PM #5
Member
- Join Date
- May 2008
- Location
- Ohio
- Posts
- 20
- Rep Power
- 0
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 07:07 PM. Reason: Thanks
- 05-22-2008, 08:27 PM #6
Member
- Join Date
- Dec 2007
- Posts
- 17
- Rep Power
- 0
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
- 05-23-2008, 03:59 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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());
- 05-23-2008, 08:34 PM #8
Member
- Join Date
- May 2008
- Location
- Ohio
- Posts
- 20
- Rep Power
- 0
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 theJava Code:System.out.printf("Item Price: $%,.2f\n " + inventory[i].getItemPrice()); System.out.printf("Value of Inventory: $%,.2f\n " + inventory[i].getValue());
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:Java Code:$%,.2f\n
Thanks again for the help. I really do appriciate it.Last edited by Azzia; 05-23-2008 at 08:36 PM. Reason: Thanks again!
- 05-23-2008, 08:59 PM #9
Member
- Join Date
- May 2008
- Location
- Ohio
- Posts
- 20
- Rep Power
- 0
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.
- 05-23-2008, 10:11 PM #10
Member
- Join Date
- May 2008
- Location
- Ohio
- Posts
- 20
- Rep Power
- 0
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
doesn't work either. I keep gettingJava Code:System.out.printf("Item Price: $%.2f\n" + inventory[i].getItemPrice()); System.out.printf("Value of Inventory: $%.2f\n" + inventory[i].getValue());
I am terrible at reading errors too. Hmmm. Back to work.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)
- 05-23-2008, 10:16 PM #11
Member
- Join Date
- May 2008
- Location
- Ohio
- Posts
- 20
- Rep Power
- 0
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:
Where the restocking fee needs to be formated. Back to work again.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
- 05-24-2008, 04:26 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
That's what I want to pointed you. Formatted in wrong way. Now everything id fine, right? If you solve the problem please mark the thread solved.
Similar Threads
-
Cannot get passed these syntax errors
By MrKP in forum New To JavaReplies: 1Last Post: 05-12-2008, 07:05 AM -
help with these errors
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-16-2008, 04:55 PM -
Errors I don't understand
By MattyB in forum New To JavaReplies: 4Last Post: 04-01-2008, 11:55 PM -
I have 3 errors after compiling
By coco in forum JDBCReplies: 2Last Post: 10-18-2007, 09:32 AM -
Errors in constructor
By ai_2007 in forum Advanced JavaReplies: 0Last Post: 07-01-2007, 05:35 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks