Jump to content

Problem with printing an object

- - - - -

  • Please log in to reply
18 replies to this topic

#1
p03p

p03p

    Newbie

  • Members
  • PipPip
  • 12 posts
Im trying to do the followng code:

out.printf("%s", one.verschil(two));

But when im printing it i get the output with the object name followed by @293c or something. What am i missing? I also tried to print it as a char but also didnt work.

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
The class where one and two is made of needs a tostring method
public String toString(){ 

    return "this will print";

}

Or the verschil method returns something faulty.

#3
p03p

p03p

    Newbie

  • Members
  • PipPip
  • 12 posts
I did a test by pasting

public String toString(){ 
    return "this will print";
}

And it seems to work, and prints "this will print". Now i wanna get it to print the difference(verschil) so i watched some tutorials about toString. But i dont really get it. What should i place in "this will print"? I think think that it should the result of one.verschil(two);

#4
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
verschil must return the String you want to print

#5
p03p

p03p

    Newbie

  • Members
  • PipPip
  • 12 posts
Ill try to give a more complete code this is what i do:
	private void printOperaties() {
		out.printf("Verschil = %s\n", one.verschil(two));
	}

And in the class which make one and two i did:
public Verzameling verschil(Verzameling v) {
	    Verzameling verschilVerzameling = new Verzameling();
	    for (int i=0; i<this.size(); i++) {
		    if (!v.inSet(identifierArray[i])) {
			    verschilVerzameling.addElement(identifierArray[i]);    
		    }
	    }
	    return verschilVerzameling;
    }

public String toString(){ 
    return "this will print";
}

So i assume i have to get verschilVerzameling into the toString? But whatever i put in there it gives me error.

#6
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I'm assuming you want to print some property of the "Verzameling" object that is the result of the operation "verschil()".
If this is what you want to do you must put the toString method in the Verzameling class.
Something like:

class Verzameling{

   ...

   ...


   public String toString(){

      String x="Object Verzameling. This is the object number " + this.number + " and my name is " + this.name;

      return x;

   }



}

Of course I just made up "Verzameling.number" and "Verzameling.name". You can buld the String to return the way you want.

#7
p03p

p03p

    Newbie

  • Members
  • PipPip
  • 12 posts
This is what i want to do:
The user will input some words 2 times. So first time can be "hello world" and 2nd time "hello there". So it will return "there" because it wasnt in the first input.
After that i wanna do a print on the screen of "there"

#8
p03p

p03p

    Newbie

  • Members
  • PipPip
  • 12 posts
So i have this is the verzameling class now:

    private Identifier[] identifierArray;
    private int aantalElementen;

    Verzameling () {
	aantalElementen = 0;
	identifierArray = new Identifier[MAXIMAAL_AANTAL_ELEMENTEN];
    }

    public Verzameling verschil(Verzameling v) {
	    Verzameling verschilVerzameling = new Verzameling();
	    for (int i=0; i<this.size(); i++) {
		    if (!v.inSet(identifierArray[i])) {
			    verschilVerzameling.addElement(identifierArray[i]);    
		    }
	    }
	    return verschilVerzameling;
    }

	public String toString(){ 
		String x = "Testing: "+ identifierArray;
		return x;
	}

But im still getting the weird hex like output.

#9
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts

public class Verzameling {



        public String toString(){ 

		String x = "Testing: ";

                   for(int i=0;i<identifierArray.length;i++)

                      x+=identifierArray[i].something+" "; //you have to print a variable in the Identifier class I guess

		

                   return x;

	}


}


#10
p03p

p03p

    Newbie

  • Members
  • PipPip
  • 12 posts
Im feeling so noob now :P trying to understand the code u gave. So in the for loop its checking each spot in identifierArray[]

But what do you mean with something in identifierArray[i].something and //you have to print a variable in the Identifier class I guess. My programming knowledge is very limited as u see :P

#11
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
I mean that you probably want to post a field (variable) for each element in the identifierArray array.
Can you post the Identifier class if it's yours?

#12
p03p

p03p

    Newbie

  • Members
  • PipPip
  • 12 posts
This is the identifier class, also wanna note that i didnt post all the verzameling class code

import java.util.regex.Pattern;

class Identifier implements IdentifierInterface {

    private char[] charArray;
    private int aantalElementen;

    public Identifier () {
	aantalElementen = 0;
	charArray = new char[100];
    }

    public Identifier (Identifier element) {
	aantalElementen = element.aantalElementen;
	charArray = new char[element.size()];	
	kopieerElementen(charArray, element.charArray, aantalElementen);
    }

    private void kopieerElementen (char[] dest, char[] src, int aantal) {
	for (int i = 0; i < aantal; i++) {
	    dest[i] = src[i];
	}
    }

    public void init (char c) {
	aantalElementen=0;
	charArray[aantalElementen]=c;
    }

    public int size () {
	return aantalElementen;
    }

    public boolean isIdentical (Identifier element) {
	if(aantalElementen!=element.aantalElementen) { 
		return false;
	}
	else {
	
		for (int i=0; i<aantalElementen; i++) {
			if (charArray[i]!=element.charArray[i]) {
				return false;
			}
		}
		return true;
	}
    }
    
    public void addChar (char c) {
	charArray[aantalElementen] = c;
	aantalElementen+=1;
    }

    public char getChar (int positie) {
	return charArray[positie];
    }






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users