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

    Default

    i think i got one step closer:

    this is the code in the listener where i get and write the data:

    Java Code:
                            //Construct the BufferedWriter object
                            bufferedWriter = new BufferedWriter(new FileWriter(file));
    
                            //Start writing to the output stream
                            bufferedWriter.write(file.getName());
                            bufferedWriter.newLine();
                            bufferedWriter.write(Integer.toString(gameController.game.size));
                            bufferedWriter.newLine();
                            for(Cage newcage:gameController.game.gamecages){
                                bufferedWriter.write(newcage.toString());
                                bufferedWriter.newLine();
                            }
                            bufferedWriter.write("end"+file.getName());
    and the output is

    test1.txt
    4
    [appClientModule.model.Cell@19f488f1, appClientModule.model.Cell@54a9387, appClientModule.model.Cell@631b86c7, appClientModule.model.Cell@242da5a6]
    [appClientModule.model.Cell@30d647d8, appClientModule.model.Cell@4406cef4, appClientModule.model.Cell@1afd1810]
    [appClientModule.model.Cell@5e36d047, appClientModule.model.Cell@60a415a3, appClientModule.model.Cell@3488b1e6]
    [appClientModule.model.Cell@7a578dfb, appClientModule.model.Cell@a2a987d, appClientModule.model.Cell@72813bc1, appClientModule.model.Cell@27a36a2]
    [appClientModule.model.Cell@2598c6f3, appClientModule.model.Cell@512d8ecd]
    endtest1.txt

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

    Default

    Do the Cell objects have toString() methods? The output is like you got earlier. You're getting closer.

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

    Default

    No without toString() method ... i don't know how to add that method so it would work ... if I put it in Cage class, i can't acces it being static ... if somehow i manage to acces it it will give me that error with Null...
    I also tried finding more examples on internet about this but nothing that would work in my case ...
    I don't know ... if you can give an example on how to implement that ... but i don't want to ask to much ...

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

    Default

    Here is a sample toString method:
    Java Code:
    class AClass {
       String data;
       int val;
      ....
      @Override
      public String toString() {
          return "AClass: data="  + data + ", val=" + val);  // Return some identifying info about this instance
      }
    } // end class AClass

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

    Default

    i did this :

    @Override
    public String toString() {
    return targetNumber+" "+operator+" "+size+" "+cells;
    }

    and the result is

    test1.txt
    5
    75 * 4 [appClientModule.model.Cell@6e848ecc, appClientModule.model.Cell@40bb2bc3, appClientModule.model.Cell@40363068, appClientModule.model.Cell@25a41cc7]
    4 / 2 [appClientModule.model.Cell@395d601f, appClientModule.model.Cell@2151b0a5]
    12 * 3 [appClientModule.model.Cell@7a2431b9, appClientModule.model.Cell@5e3ca754, appClientModule.model.Cell@600dac21]
    20 * 2 [appClientModule.model.Cell@219fdbcb, appClientModule.model.Cell@616fdac]
    12 + 4 [appClientModule.model.Cell@4f682a53, appClientModule.model.Cell@3fb2d7df, appClientModule.model.Cell@767a9224, appClientModule.model.Cell@6910b640]
    7 + 3 [appClientModule.model.Cell@482d59a3, appClientModule.model.Cell@1958015a, appClientModule.model.Cell@60c0c8b5]
    4 = 1 [appClientModule.model.Cell@10b914b3]
    6 * 2 [appClientModule.model.Cell@66fdfc58, appClientModule.model.Cell@2cc22a3b]
    6 + 2 [appClientModule.model.Cell@7f629e71, appClientModule.model.Cell@23904e0d]
    8 * 2 [appClientModule.model.Cell@56f449b8, appClientModule.model.Cell@4013fba1]
    endtest1.txt

    cells is declared like this: public List<Cell> cells = new ArrayList<Cell>();
    Last edited by Johnny22; 06-24-2011 at 10:26 PM.

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

    Default

    a friendly introduction to the solution through a long discussion does not seem to work. I've decided to list one example:

    Java Code:
    public class Cell {
    	private int x;
    	private int y;
    	
    	// The constructor for the class Cell
    	public Cell (int x, int y) {
    		this.x = x;
    		this.y = y;
    	}
    	
    	// without overriding toString the output will be something like "cell.Cell@3e25a5"
    	// but, overriding the output will be "Cell: x=10, y=20" according to the return string 
    	public String toString() {
    		return "Cell: x=" + x + ", y=" + y; 
    	}
    	
    	public static void main(String[] args) {
    		Cell cell1 = new Cell(10, 20);
    		System.out.println(cell1.toString());
    	}
    }

    it shoudln't be hard to adapt the example to your cage class.

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

    Default

    Java Code:
    public String toString() {
    return targetNumber+" "+operator+" "+size+" "+cells;
    }
    In what class did you put that code?

    appClientModule.model.Cell@6e848ecc,
    This output shows that the appClientModule.model.Cell class does NOT have a custom toString method.

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

    Default

    public String toString() {
    return targetNumber+" "+operator+" "+size+" "+cells;
    }
    is in Cage class

    i need to do another overwrite to that method in Cell class ?

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

    Default

    i need to do another overwrite to that method in Cell class ?
    You need one in EVERY SINGLE CLASS that you want to see info about the contents of the class when you print a reference to a class object. Otherwise you get the default Object class's output.

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

    Default

    I'm almost done, i managed to get them to show, but i have some other data that i don't want there ...
    Cell
    Java Code:
    	@Override
            public String toString() {
                String xnew = null;
                    if(x == 1)
                        xnew = "A";
                    else if(x == 2)
                        xnew = "B";
                else if(x == 3)
                        xnew = "C";
                else if(x == 4)
                        xnew = "D";
                else if(x == 5)
                        xnew = "E";
                else if(x == 6)
                        xnew = "F";
                else if(x == 7)
                        xnew = "G";
                else if(x == 8)
                        xnew = "H";
                else if(x == 9)
                        xnew = "I";
    		return " "+xnew+y;
    	}
    Cage
    Java Code:
        @Override
            public String toString() {
    	    return targetNumber+" "+operator+" "+size+" "+cells;
            }
    FileSaveListener
    Java Code:
                            bufferedWriter.write(file.getName());
                            bufferedWriter.newLine();
                            bufferedWriter.write(Integer.toString(gameController.game.size));
                            bufferedWriter.newLine();
                            Cell cell = new Cell();
                            for(Cage newcage:gameController.game.gamecages){
                                   String   tmpString = newcage.toString().replace( ',' , ' ' );
    
                                    bufferedWriter.write(tmpString+" ");
                            }
                            bufferedWriter.write("end"+file.getName());
    test1.txt
    5
    3 = 1 [A1]
    20 * 2 [A2 A3]
    11 + 3 [A4 B4 B3]
    1 = 1 [A5]
    7 + 4 [B1 B2 C2 C3]
    5 + 2 [B5 C5]
    10 + 3 [C1 D1 D2]
    45 * 4 [C4 D4 E4 D3]
    5 = 1 [D5]
    10 * 2 [E1 E2]
    1 = 1 [E3]
    4 = 1 [E5]
    endtest1.txt

    how do i remove '[' and ']' ... it's very important ... if it cannot be made, i will just modify the open method :) ...

    Also many thanks for all the help ...
    Last edited by Johnny22; 06-25-2011 at 12:52 AM.

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

    Default

    how do i remove '[' and ']'
    Save the results of the toString() method in a String and use substring() to strip them.

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

    Default

    couldn't make it work with substring ... but i managed to remove ',' with replace :D

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

    Default

    I guess we can't help you if you don't show the code that doesn't work.

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

    Default

    i don't mean it's not working ...i mean it doesn't do what i need .... i did something like ...
    Java Code:
    String newstr = newcage.toString();
                                    bufferedWriter.write(newstr.substring(0)+" ");
    i needed to put 0 because that indicates from where to start ... if i leave it empyt, it's not working, if i put something else, it starts from that index ... but the brackets, are allways there ...

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

    Default

    Java Code:
    The string:   [ABCD]
    The indexes:  012345
    If you substring the above string from index = 1 thru index = 4(length-1) you strip off the []s

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

    Default

    yes, but now i have strings like this "11 + 3 [A4 B4 B3]", the ']' i can remove, but the '[' i don't know where it is, the string will be different ...

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

    Default

    I removed the '[', ']' and ',' ... like this ...
    Java Code:
       String   reml = newcage.toString().replaceAll( "\\[" , "" );
                                     String remr = reml.substring(0,reml.length()-1);
                                     String remf = remr.replaceAll(",","");
                                    bufferedWriter.write(remf+"");

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

    Default

    I removed the '[', ']' and ',' ... like this ...
    Java Code:
       String   reml = newcage.toString().replaceAll( "\\[" , "" );
                                     String remr = reml.substring(0,reml.length()-1);
                                     String remf = remr.replaceAll(",","");
                                    bufferedWriter.write(remf+"");

Page 2 of 2 FirstFirst 12

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
  •