Results 1 to 6 of 6
  1. #1
    baap30 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default Having an issue with this

    i have a program for an Inventory project using arrays no errors on this but the output is 3 lines of this

    //Product@1a8c4e7
    Product@1a8c4e7
    Product@1a8c4e7

    this is my code
    code

    Java Code:
    // class inventory1{
    	
    	
    
    
    private String name;//product name 
    private int number;//item number 
    private int units;//units available 
    private double price;//price per unit 
    
    
    public void Inventory1 () //Default constructor 
    { 
    
    }//End default constructor Product 
    
    
    //Initialization Constructor 
    public void Inventory1 ( String nameIn, int numberIn, int unitsIn, double priceIn) 
    { 
    
    setName (nameIn); 
    setNumber (numberIn); 
    setUnits (unitsIn); 
    setPrice (priceIn); 
    //setValue (valueIn); 
    
    } //End initialization constructor Product 
    
    
    //Declare Set/Get Methods 
    public void setName( String nameInput ) 
    { 
    
    name = nameInput; 
    
    
    } //End method setName 
    
    
    
    public void setNumber ( int numberInput ) 
    { 
    
    } //End method setNumber 
    
    
    public void setUnits ( int unitsInput ) 
            { 
            units = unitsInput; 
            } 
            public void setPrice ( double priceInput ) 
            { 
            price = priceInput; 
            } //End method setPrice 
    
            public String getName () 
            { 
            return ( name); 
            } //End method getName 
    
            public double getNumber () 
            { 
            return ( number ); 
    } //End method getNumber 
    
            public double getUnits () 
            { 
            return ( units ); 
    } //End method getUnits 
    
            public double getPrice () 
            { 
            return ( price ); 
    } //End method getPrice 
    
            public double InventoryValue() 
    { 
    return ( units * price ); 
    
    } //End method InventoryValue 
    
    //toString()Method 
    
    //Returns a formatted String for output purposes 
    
    public String name() 
    { 
             String formatString    =  "Identification Number "+getNumber()+"\n"; 
             formatString                    += " Product Name               "+getName()+"\n"; 
             formatString                      += " Units In Stock     "+getUnits()+"\n"; 
             formatString                      += " Unit Price                 "+getPrice()+"\n"; 
             formatString              += "Inventory Value            "+InventoryValue()+"\n"; 
    
              return formatString; 
    
    }//End name() 
    
    
    }//End Inventory1
    
    
    ////
    public class Product
    { 
            public Product() {
    		// TODO Auto-generated constructor stub
    	}
    
    		public Product(String string, int i, int j, double d) {
    			// TODO Auto-generated constructor stub
    		}
    
    		public static void main( String args[] ) 
            { 
    //Declare and initialize the inventory array 
      Product inventory []; //An array variable 
     
      //Size the array 
      inventory = new Product[3]; 
     
      inventory[0] = new Product("Red",01, 200,2.99); 
      inventory[1] = new Product("Blue", 02, 251, 3.99); 
      inventory[2] = new Product("Yellow", 3, 1000, 2.99); 
     
    //Sort the array Product.sort( inventory ); //
     
    int x = inventory[0].compareTo(inventory[1]); 
     
    System.out.println(x+"\n"); 
     
     
    for(int i=0; i<3; i++) 
    System.out.println(inventory[1].toString()); 
    }//End main 
    
    		private int compareTo(Product product) {
    			// TODO Auto-generated method stub
    			return 0;
    		}
     
    }//End
    Moderator Edit: Code tags added
    Last edited by Fubarable; 04-23-2010 at 02:50 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I've added code tags to your post to try to help show the code's formatting, but unfortunately your formatting is poor, and this makes it hard for others (namely us) to read and analyze your code. If you desire our help, it would be in your best interest to reformat your code and then edit your first post and repost the code.


    From the little I can understand, you seem to have a class called inventory1 that has bad constructors (constructors should have no return type, not even void), but at least has a toString() method, but I don't see where you ever use inventory1 objects. You do use Product objects and even try to call toString() on these objects but haven't given this class a decent toString() method (that's the cause of your error).

    If you fix up your code, maybe we can tell you more.

    Best of luck and welcome to the forum.

  3. #3
    baap30 is offline Member
    Join Date
    Apr 2010
    Posts
    5
    Rep Power
    0

    Default

    here is the addition of the missing code but the results are the same

    code:
    class inventory1
    {




    private String name;//product name
    private int number;//item number
    private int units;//units available
    private double price;//price per unit


    public void Inventory1 (){ //Default constructor


    }//End default constructor Product


    //Initialization Constructor
    public void Inventory1 ( String nameIn, int numberIn, int unitsIn, double priceIn)
    {

    setName (nameIn);
    setNumber (numberIn);
    setUnits (unitsIn);
    setPrice (priceIn);
    //setValue (valueIn);

    } //End initialization constructor Product


    //Declare Set/Get Methods
    public void setName( String nameInput )
    {

    name = nameInput;


    } //End method setName



    public void setNumber ( int numberInput )
    {

    } //End method setNumber


    public void setUnits ( int unitsInput )
    {
    units = unitsInput;
    }
    public void setPrice ( double priceInput )
    {
    price = priceInput;
    } //End method setPrice

    public String getName ()
    {
    return ( name);
    } //End method getName

    public double getNumber ()
    {
    return ( number );
    } //End method getNumber

    public double getUnits ()
    {
    return ( units );
    } //End method getUnits

    public double getPrice ()
    {
    return ( price );
    } //End method getPrice

    public double InventoryValue()
    {
    return ( units * price );

    } //End method InventoryValue

    //toString()Method

    //Returns a formatted String for output purposes

    public String name()
    {
    String formatString = "Identification Number "+getNumber()+"\n";
    formatString += " Product Name "+getName()+"\n";
    formatString += " Units In Stock "+getUnits()+"\n";
    formatString += " Unit Price "+getPrice()+"\n";
    formatString += "Inventory Value "+InventoryValue()+"\n";

    return formatString;

    }//End name()


    //End Inventory1
    import java.util.Scanner;

    public class Inventory

    {
    //main methods begins execution of application
    public static void main (String args [] )

    {

    Scanner input = new Scanner (System.in );
    Product p = new Product ( );


    System.out.printf (p.toString () );
    }
    }//End InventoryApplication

    desperate to solve this

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hey, I added code tags the last time, if you read the link in my signature, you can learn how to edit your post above and add them yourself. All of us here will thank you for doing this because it will make reading your code that much easier.

    Best of luck!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    the main error you have in your code is already been mentioned by fubarable

    Java Code:
    (constructors should have no return type, [b]not even void)[/b]

    and i renamed your method name() to toString() so that this Object method is overriden and returns you string concatenation instead of a strange hashcode;). here is the the code that produce this output

    Identification Number 111.0
    Product Name Product 1
    Units In Stock 50.0
    Unit Price 500.0
    Inventory Value 25000.0

    i don't used the scanner, instead i instantiate the inventory directly. so with this code you can store only one product. but you can easily enhance it by adding a collection for your products and the appropriate methods add, remove and so on.


    Java Code:
    package myjava.examples.org;
    
    class Inventory {
    
    	private String name;// product name
    	private int number;// item number
    	private int units;// units available
    	private double price;// price per unit
    
    	public Inventory(String nameIn, int numberIn, int unitsIn, double priceIn) {
    
    		setName(nameIn);
    		setNumber(numberIn);
    		setUnits(unitsIn);
    		setPrice(priceIn);
    		// setValue (valueIn);
    
    	} // End initialization constructor Product
    
    	// Declare Set/Get Methods
    	public void setName(String nameInput) {
    
    		name = nameInput;
    
    	} // End method setName
    
    	public void setNumber(int numberInput) {
    		number = numberInput;
    
    	} // End method setNumber
    
    	public void setUnits(int unitsInput) {
    		units = unitsInput;
    	}
    
    	public void setPrice(double priceInput) {
    		price = priceInput;
    	} // End method setPrice
    
    	public String getName() {
    		return (name);
    	} // End method getName
    
    	public double getNumber() {
    		return (number);
    	} // End method getNumber
    
    	public double getUnits() {
    		return (units);
    	} // End method getUnits
    
    	public double getPrice() {
    		return (price);
    	} // End method getPrice
    
    	public double InventoryValue() {
    		return (units * price);
    
    	} // End method InventoryValue
    
    	// toString()Method
    
    	// Returns a formatted String for output purposes
    
    	public String toString() {
    		String formatString = "Identification Number " + getNumber() + "\n";
    		formatString += " Product Name " + getName() + "\n";
    		formatString += " Units In Stock " + getUnits() + "\n";
    		formatString += " Unit Price " + getPrice() + "\n";
    		formatString += "Inventory Value " + InventoryValue() + "\n";
    
    		return formatString;
    
    	}// End name()
    	
    	public static void main (String args [] )
    
    	{
    
    	Inventory p = new Inventory ("Product 1", 111, 50, 500.00 );
    
    
    	System.out.printf (p.toString () );
    	}
    	}//End InventoryApplication

Similar Threads

  1. nio issue
    By mawandiadeepak in forum Networking
    Replies: 2
    Last Post: 03-17-2010, 05:23 AM
  2. JSF issue
    By premjo in forum New To Java
    Replies: 0
    Last Post: 02-14-2010, 02:19 PM
  3. Issue
    By FlashNinja in forum New To Java
    Replies: 20
    Last Post: 11-28-2009, 09:44 PM
  4. PDF Box issue
    By jazz2k8 in forum Advanced Java
    Replies: 0
    Last Post: 03-20-2009, 11:04 AM
  5. Issue
    By chaitu444 in forum New To Java
    Replies: 2
    Last Post: 11-06-2007, 07:49 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
  •