Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-21-2008, 09:42 PM
Member
 
Join Date: May 2008
Location: Ohio
Posts: 20
Azzia is on a distinguished road
Send a message via AIM to Azzia Send a message via MSN to Azzia
[SOLVED] Expected errors, I can't see what's wrong.
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.

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-22-2008, 01:16 AM
Member
 
Join Date: Apr 2008
Posts: 23
theonly is on a distinguished road
I think your problem is in your concatenation instead of using "," in between "Item Name" use a "+" and so on.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-22-2008, 01:34 AM
orchid's Avatar
Member
 
Join Date: Apr 2007
Location: Midwest
Posts: 60
orchid is on a distinguished road
You are missing a comma here after this :
"%s: %s\n %s: %.2f\n %s: %.2f\n %s: %.2f"
see below:
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()); }
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-22-2008, 07:28 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-22-2008, 09:06 PM
Member
 
Join Date: May 2008
Location: Ohio
Posts: 20
Azzia is on a distinguished road
Send a message via AIM to Azzia Send a message via MSN to Azzia
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-22-2008, 10:27 PM
Member
 
Join Date: Dec 2007
Posts: 17
Jman is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-23-2008, 05:59 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 05-23-2008, 10:34 PM
Member
 
Join Date: May 2008
Location: Ohio
Posts: 20
Azzia is on a distinguished road
Send a message via AIM to Azzia Send a message via MSN to Azzia
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
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>

Thanks again for the help. I really do appriciate it.

Last edited by Azzia : 05-23-2008 at 10:36 PM. Reason: Thanks again!
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 05-23-2008, 10:59 PM
Member
 
Join Date: May 2008
Location: Ohio
Posts: 20
Azzia is on a distinguished road
Send a message via AIM to Azzia Send a message via MSN to Azzia
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-24-2008, 12:11 AM
Member
 
Join Date: May 2008
Location: Ohio
Posts: 20
Azzia is on a distinguished road
Send a message via AIM to Azzia Send a message via MSN to Azzia
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
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

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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-24-2008, 12:16 AM
Member
 
Join Date: May 2008
Location: Ohio
Posts: 20
Azzia is on a distinguished road
Send a message via AIM to Azzia Send a message via MSN to Azzia
Alrighty. I changed them all to:

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:

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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 05-24-2008, 06:26 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot get passed these syntax errors MrKP New To Java 1 05-12-2008 09:05 AM
help with these errors oceansdepth New To Java 3 04-16-2008 06:55 PM
Errors I don't understand MattyB New To Java 4 04-02-2008 01:55 AM
I have 3 errors after compiling coco Database 2 10-18-2007 11:32 AM
Errors in constructor ai_2007 Advanced Java 0 07-01-2007 07:35 PM


All times are GMT +3. The time now is 08:12 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org