• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructors objects  RSS feed

 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a method that accepts as a parameter an object:


Client is a class which has a constructor that has two String parameters:


In the main add a Client in this way:


Now, i have an array of Client, so I would like to enter within this.
Example:


I have used the println for check if worked insertion, but the result shows no
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What DOES it print?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it prints something like Client@15db9743 and you want something more descriptive you have to override the toString() method.
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had tried it to see if it would work for what I should do, and that is put into an array of clients, some "customers" with name passed from the main
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had tried it to see if it would work for what I should do, and that is put into an array of clients, some "customers" with name passed from the main

Sorry but I'm not sure what you mean by this.

Do as Carey has suggested and override the toString() method in your Cliente class.

If you don't understand what this means try googling for something like "Java Overriding toString()", there's lots of help on this subject such as http://www.javabeginner.com/learn-java/java-tostring-method
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
I had tried it to see if it would work for what I should do, and that is put into an array of clients, some "customers" with name passed from the main

Sorry but I'm not sure what you mean by this.

Do as Carey has suggested and override the toString() method in your Cliente class.

If you don't understand what this means try googling for something like "Java Overriding toString()", there's lots of help on this subject such as http://www.javabeginner.com/learn-java/java-tostring-method




result of print:
null

Instead, should appear name of client :/
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which class have you put that code in?
If you are running the original code you showed it can't possibly just print "null" it has to print "test " followed by something else.

Please give all the information.
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Which class have you put that code in?
If you are running the original code you showed it can't possibly just print "null" it has to print "test " followed by something else.

Please give all the information.


Sorry, appear: Client null

I put him in the class Client
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:
result of print:
null

Instead, should appear name of client :/


Your snippet of Client only shows members: name, and lastname. Where did the variable 'clients' come from? What is its data type? Your toString() method should only utilize 'name' and 'lastname' and toString() should be declared inside the Client class.
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:

Your snippet of Client only shows members: name, and lastname. Where did the variable 'clients' come from? What is its data type? Your toString() method should only utilize 'name' and 'lastname' and toString() should be declared inside the Client class.


Yes, the variables are String type and belong to Client class.

Howover, with override of toString() I get always the result: name@1234


 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manu

Can you please post the entire Client class?
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to print an object, but in reality what should be printed?

James Boswell wrote:Manu

Can you please post the entire Client class?


 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your implementation of toString changes every time you post. It is very difficult to help you. Your current implementation means you need to override toString in the Conto class, assuming this is what you actually want.
 
manu lix
Ranch Hand
Posts: 74
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:Your implementation of toString changes every time you post. It is very difficult to help you. Your current implementation means you need to override toString in the Conto class, assuming this is what you actually want.


Sorry

That's because I was working on this at the moment.
However, it is the same thing!
In any event, my question remains the same.
 
subodh kureel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code you post does not seems good and has compilation issue . However toString() should help you out to print the values you are looking for.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
manu lix wrote:That's because I was working on this at the moment.
However, it is the same thing!
In any event, my question remains the same.

The problem we have is we are currently trying to hit a target that we can't see and if that wasn't difficult enough it is moving around.
We can only give advice on the information we have ie the information you give to us and currently there is sufficient information. So please give us all the information and stop changing the code between posts without telling us it has changed.
Also please be precise, for example you have a class called Cliente but keep referring to Client - is this supposed to be the same class and you are translating the name or is it a different class altogether?

The code you showed which had a toString method that printed out "clients" - what class is this code in and can you show the code for that class because my guess is that is where the problem really lies.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!