Hello,
I'm trying to print a string value but I can't seem to do it:
what I get is appClientModule.model.Cage@4c29d65b
I also tried converting with toString() and the result is the same.
Printable View
Hello,
I'm trying to print a string value but I can't seem to do it:
what I get is appClientModule.model.Cage@4c29d65b
I also tried converting with toString() and the result is the same.
if you want to see the content of your object your must override the method toString with
public String toString() {}
and this method must return your string. perhaps you can post your object.
appClientModule.model.Cage@4c29d65b
That is the default output of the Object class's toString() method. If you don't override it that is what you'll get.
So I used this to try to get the cages: bufferedWriter.write("test"+gameController.game.ga mecages);//cages.
gamecages is declared in another class like this : public List<Cage> gamecages = new ArrayList<Cage>();.
The result i get is test[appClientModule.model.Cage@40363068, appClientModule.model.Cage@25a41cc7, appClientModule.model.Cage@395d601f, appClientModule.model.Cage@2151b0a5, appClientModule.model.Cage@7a2431b9, appClientModule.model.Cage@5e3ca754, appClientModule.model.Cage@600dac21, appClientModule.model.Cage@219fdbcb, appClientModule.model.Cage@616fdac]
and i need to the values that saved for each cage ...
Did you add a toString method to the Cage class?
Be sure to have the compiler check if you coded the definition correctly by adding @Override just before its definition.
i don't know how should i do it ... can you give me an example on how to implement that ?
Are you saying you do not know how to write a method that returns a String?Quote:
i don't know how should i do it
Read the API doc for the Object class and look at the format for its toString method?
Put whatever String you want to see after the return statement in the toString method:
return "Cage: x=" + x + ", y=" + y;
Cage
GameCode:public int targetNumber ;
public String operator ;
public int size ;
public ArrayList<int[]> PossibleCombinations = new ArrayList<int[]>();
private Game game ;
private List<Cell> cells = new ArrayList<Cell>();
private int[] currentCombination ;
private int[] TimesArray ;
I deleted the rest of the code and left only the declarations, maybe these will help, i don't know what to post so that may help you understand what i did till now ...Code:public int size ;
public boolean isValid ;
public List<Cage> gamecages = new ArrayList<Cage>();
private List<List<Cell>> solutionsCells = new ArrayList<List<Cell>>();
private ListIterator<Cage> gamecagesitr ;
Till now i managed to get only the gamesize :)) .
The operator and target number i think are done in the same way, but my biggest problem is getting the cells for the cage:
In the file i should have something like this "2 - 2 A4 B4" 2->target number, - ->operator, 2 -> cagesize, A4 B4 -> cells of the cage. Also every new cage on a new line ...
Why are you dumping all that code here? I for one, am not going to check it all. There is no toString() method in it anyway, so what do you expect to be printed?
kind regards,
Jos
I edited that post, and deleted the code ...
i tried to use toString() but the output is the same ... i think they are allready converted into string ...
Have you read what we posted? We said:Quote:
i tried to use toString()
add a toString() method to the Cage class?
Not call the toString() method. That will only return what the default method does. Give you the name of the class and its memory address.
and what to write into toString() method ?
Whatever you want to see when you try to print a reference to the class.
See post#7 for an example.
I tried to do something for a test but it says that a non-static method toString() cannot be referenced from a static context.
When you get errors, please post the full text of the error message and the code.
I have no idea how you coded it?
If you add a method to a class, you need an object to call one of its methods unless the method is static. But toString() is not static.
I added this method into Cage
and into the listener where i also try to get the data and save it into the fileCode:@Override
public String toString() {
List<Cell> listofcells = getCells();
return listofcells.toString();
}
i added:
andCode:Cage cage;
and now i getCode:bufferedWriter.write(cage.toString());
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at appClientModule.controller.listeners.FileSaveListe ner.actionPerformed(FileSaveListener.java:48)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
[...more errors but for the same thing...]
Sorry, I don't understand what the Cage class contains. Does it have a few properties that you'd like to see when the class's object is printed?
What objects are in the List returned by the getCells() method?
The List.toString() reference is going to call the List class's toString() method.
What code is at line 48 in the FileSaverListener class? What variable in that line is null?
I can't extract some code, it won't compile, i did my application something like MVC and everything is connected. If I remove some of them, the rest won't work...
What code is at line 48 in the FileSaverListener class? What variable in that line is null? The line 48 is that bufferedWriter.write(cage.toString());
What objects are in the List returned by the getCells() method? Cell objectsI will try and explain what i did : My application creates random grids, that contain cages, every cage contains an math operator, an target number and some cells...Code:public List<Cell> getCells(){
return this.cells ;
}
What i'm trying now to do is to save that grid details into a file so i can open it later ... the method that opens an definition works ok ...
the file i want to look like this:
2 = 1 A1
7 + 2 A2 A3
2 - 2 A4 B4
Also evey Cage will contain Cells ... Every cell will contain the coords ... like X and Y.