getting to read a String out of a class in an object ?

Hi everyone ..

I'm stuck in this, and i've tried many possible solutions ..

but here is my actual question :

i've got a class "Model" which holds : names (String), address (String), sizes (int, int ,int) .... and has the following methods: getName(), getSizes(), getAdress()

then i created a class "Models" wich holds an ArrayList of all the models

here i can : addModel(), removeModel(), showName()

everything works fine , except when i try to get the name from the models ... This is do way i tried to do this

publicvoid showNames(){

int i;

for (i=0 ; i<modellen.size() ; i++)

{

System.out.println(model.get(i));

}

}

The output is then instead of names, the references to the objescts (eg. : "Model@5b0668" )

What should i do to get the actual names (String) ?>

[1106 byte] By [jeroenQuanta] at [2007-11-27 11:45:20]
# 1

are you iterating through the list of Model in your loop?

if so, then when you call get(i) .. that is returning an object of class Model.

So in order to get the name of the model, you would do somethign like this

public void showNames()

{

for (int i=0 ; i<modellen.size() ; i++)

System.out.println(model.get(i).getName());

}

>

smithdale87a at 2007-7-29 18:01:09 > top of Java-index,Core,Core APIs...
# 2

Hi,

Override the toString() method in your "Model" class, where in you return the "name" instance variable.

<code>

public String toString()

{

return name;

}

</code>

Regards,

Anand

aks_rudraa at 2007-7-29 18:01:09 > top of Java-index,Core,Core APIs...
# 3

> Hi,

>

> Override the toString() method in your "Model" class,

> where in you return the "name" instance variable.

>

> <code>

> public String toString()

> {

>return name;

>

> </code>

>

> Regards,

> Anand

Terrible idea

georgemca at 2007-7-29 18:01:09 > top of Java-index,Core,Core APIs...
# 4

Thank you very much for your replies already !

the thing is, i've tried both those ways :

-getName() way : the compiler sais that there is not such method.

-toString way : returns the same value as without it : " Model@10772 " which refeers to the allacation of the method.

i also tried an iteration... the same...

i'm still kind of out of solutions here

maybe if i show you guys the code, you will be able to see a stupid mistake i'm looking over :

public class Adres {

private String straat;

private int postNummer;

private String gemeente;

public Adres()

{

straat ="";

gemeente="";

}

public Adres(String eenStraat, String eenGemeente, int eenPostNummer)

{

straat = eenStraat;

gemeente = eenGemeente;

postNummer = eenPostNummer;

}

public void setAdres(String eenStraat, String eenGemeente, int eenPostNummer)

{

straat = eenStraat;

gemeente = eenGemeente;

postNummer = eenPostNummer;

}

public boolean printAdres()

{

if(straat=="")

{

System.out.println("Adres: Geen adres");

} else {

System.out.println("Adres: " + straat + " " + postNummer + " " + gemeente);

}

return true;

}

public class Maten {

private int borstOmtrek;

private int taille;

private int heupOmtrek;

public Maten ()

{

borstOmtrek = 90;

taille = 60;

heupOmtrek = 90;

}

public Maten(int bOmtrek, int taille, int hOmtrek)

{

borstOmtrek = bOmtrek;

taille = taille;

heupOmtrek = hOmtrek;

}

public String printMaten()

{

return ("borst omtrek: " + borstOmtrek +"\n"+ "Taille: " + taille +"\n" +"Heup omtrek: " + heupOmtrek);

}

}

}

-

public class Model {

private String naam;

private Adres adres;

private Maten maten;

public Model(String nm)

{

naam = nm;

adres = new Adres();

maten = new Maten();

}

public Model(String nm, int bomtrek,int tnaille,int homtrek)

{

naam = nm;

maten = new Maten(bomtrek, tnaille, homtrek);

adres = new Adres();

}

public String getNaam()

{

return naam;

}

public void setAdres(String straat, int postcode, String gemeente)

{

adres.setAdres(straat, gemeente, postcode);

}

public void printInfo()

{

System.out.println( naam );

System.out.println(adres.printAdres());

System.out.println(maten.printMaten());

}

}

--import java.util.ArrayList;

public class Modellen {

private ArrayList modellen;

private Model model;

public Modellen() {

modellen = new ArrayList();

}

public void voegToe(Model model) {

modellen.add(model);

toonNamen();

}

public void verwijder(Model model) {

modellen.remove(model);

toonNamen();

}

/**

*

* Here is the actual problem !!!!

* if i try to get the names thruogh the getNaam() method,

* the compiler will simply say there isn't such a method !

**/

private void toonNamen() {

int i;

for (i=0 ; i<modellen.size() ; i++)

{

System.out.println(modellen.get(i));

}

}

public void toonGegevens() {

}

}

Maybe there is a very simple solution to it, but i just cant see it.>

jeroenQuanta at 2007-7-29 18:01:09 > top of Java-index,Core,Core APIs...
# 5

Well, that's true, there is no getNaam() method. Is there something preventing you from modifying the code so that there is such a method? Or is it somebody else's code and you can't change it?

DrClapa at 2007-7-29 18:01:09 > top of Java-index,Core,Core APIs...
# 6

thanks for trying

No, this is a code I wrote, although this is an exercice, my main goal was already done : adding and removing models. that works .. I didn't expect showing the names to be so difficult...

i'll ask my question a little easier :

How can i convert a java.lang.object to a string ?

my first thought on this was toString... but nothing .

then tried puting the object in a variable "m", and applied the method "m.getNaam()". as you know .. no such method(public)

jeroenQuanta at 2007-7-29 18:01:09 > top of Java-index,Core,Core APIs...
# 7

TrySystem.out.println(((Model)modellen.get(i)).getNaam());

dwga at 2007-7-29 18:01:10 > top of Java-index,Core,Core APIs...
# 8

Woooow i've been searching like crazy for an answer !

amazing ... so .. the "(Model) "points out that the "modellen.get(i) " is a model ? or is it an instruction to tell tha it has to happen in the class Model ?

again, thanks a million !

Message was edited by:

jeroenQuant

jeroenQuanta at 2007-7-29 18:01:10 > top of Java-index,Core,Core APIs...