Page 1 of 2 12 LastLast
Results 1 to 20 of 38
  1. #1
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default How to get string value

    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.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    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.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    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.

  4. #4
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    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 ...

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    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.

  6. #6
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    i don't know how should i do it ... can you give me an example on how to implement that ?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    i don't know how should i do it
    Are you saying you do not know how to write a method that returns a String?
    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;

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Johnny22 View Post
    i don't know how should i do it ... can you give me an example on how to implement that ?
    Can you show us your previous implementation? In your op you wrote:

    I also tried converting with toString() and the result is the same.
    ... so at least you attempted it ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    Cage
    Java Code:
    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 ;
    Game
    Java 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 ;
    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 ...

    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 ...
    Last edited by Johnny22; 06-24-2011 at 08:12 PM.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    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 ...

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    i tried to use toString()
    Have you read what we posted? We said:
    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.

  13. #13
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    and what to write into toString() method ?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Whatever you want to see when you try to print a reference to the class.
    See post#7 for an example.

  15. #15
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    I tried to do something for a test but it says that a non-static method toString() cannot be referenced from a static context.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    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.

  17. #17
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    I added this method into Cage
    Java Code:
        @Override
            public String toString() {
    	    List<Cell> listofcells = getCells();
                return listofcells.toString();
            }
    and into the listener where i also try to get the data and save it into the file

    i added:

    Java Code:
    Cage cage;
    and
    Java Code:
    bufferedWriter.write(cage.toString());
    and now i get

    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...]

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    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?

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Johnny22 View Post
    I tried to do something for a test but it says that a non-static method toString() cannot be referenced from a static context.
    Show us a small compilable example the shows us your problem. A guess: you can't just call toString() without having an object to call it on. Your compiler will complain about it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    Johnny22 is offline Member
    Join Date
    Jun 2011
    Posts
    31
    Rep Power
    0

    Default

    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 objects
    Java Code:
    public List<Cell> getCells(){
    		return this.cells ;
    	}
    I 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...
    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  2. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  3. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM
  4. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •