|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-21-2008, 09:42 PM
|
|
Member
|
|
Join Date: May 2008
Location: Ohio
Posts: 20
|
|
|
[SOLVED] Expected errors, I can't see what's wrong.
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.
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-22-2008, 01:16 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 23
|
|
|
I think your problem is in your concatenation instead of using "," in between "Item Name" use a "+" and so on.
|
|

05-22-2008, 01:34 AM
|
 |
Member
|
|
Join Date: Apr 2007
Location: Midwest
Posts: 60
|
|
You are missing a comma here after this :
"%s: %s\n %s: %.2f\n %s: %.2f\n %s: %.2f"
see below:
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());
}
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-22-2008, 07:28 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-22-2008, 09:06 PM
|
|
Member
|
|
Join Date: May 2008
Location: Ohio
Posts: 20
|
|
|
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 09:07 PM.
Reason: Thanks
|
|

05-22-2008, 10:27 PM
|
|
Member
|
|
Join Date: Dec 2007
Posts: 17
|
|
|
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, 05:59 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
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.
System.out.printf("Item Price: $%,.2f\n " + inventory[i].getItemPrice());
System.out.printf("Value of Inventory: $%,.2f\n " + inventory[i].getValue());
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

05-23-2008, 10:34 PM
|
|
Member
|
|
Join Date: May 2008
Location: Ohio
Posts: 20
|
|
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
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>
Thanks again for the help. I really do appriciate it.
Last edited by Azzia : 05-23-2008 at 10:36 PM.
Reason: Thanks again!
|
|

05-23-2008, 10:59 PM
|
|
Member
|
|
Join Date: May 2008
Location: Ohio
Posts: 20
|
|
|
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-24-2008, 12:11 AM
|
|
Member
|
|
Join Date: May 2008
Location: Ohio
Posts: 20
|
|
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
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
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.
|
|

05-24-2008, 12:16 AM
|
|
Member
|
|
Join Date: May 2008
Location: Ohio
Posts: 20
|
|
Alrighty. I changed them all to:
{
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:
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.
|
|

05-24-2008, 06:26 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|