• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to enter 2 parameters of the same type in a class constructor

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get an error from from java compiler when I am trying to enter a class constructor with 4 parameters 2 of which are of the same type.Any ideas how to solve this?

code :

public Invoice ( String pNumber, String pDescription, int iQuantity, double iPrice )
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code looks fine and it certainly is legal to have multiple parameters of same type. Sounds as if you may have misinterpreted the compiler's complaints. Can you post the compiler output?
 
Nick Petas
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use eclipse with sdk6 compiler.The error i get is that :

Duplicate method Invoice(String) in type Invoice
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's telling you that you have two constructors, both of which take the same parameter list, a String. So somewhere, you have

followed by

The parameter list must be different somehow. You're best bet is to take one out.
[ February 07, 2007: Message edited by: Fred Rosenberger ]
 
Nick Petas
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no other constructor in this class.Thnx for the immediate replies!
 
Nick Petas
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the complete code list so far :

public class Invoice
{
private String partNumber, partDescription; //part number and part description
private int itemQuantity; //item quantity
private double itemPrice; //item price

//constructors initialize all four instance variables
public Invoice ( String pNumber, String pDescription, int iQuantity, double iPrice )
{
partNumber = pNumber; //initializes partNumber;
partDescription = pDescription; //initializes partDescription
itemQuantity = iQuantity; //initializes itemQuantity
itemPrice = iPrice; //initializes itemPrice
} //end constructor1
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It compiles fine, with *no* errors when I tried it.
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure you're actually compiling the file you think you're compiling? Sometimes, one can get confused about directories. To be sure, rename or move the file you think you're compiling then try the compilation again. If it still compiles, you'd better find that other file!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which line does the error point to? (Just double-click on the error in the problems view.)
 
reply
    Bookmark Topic Watch Topic
  • New Topic