This site requires JavaScript, please enable it in your browser!
Greenfoot back
makconas
makconas wrote ...

2015/11/19

how to keep string data , after reset

makconas makconas

2015/11/19

#
this is my code, I want to output a string save in method save ,and use it in load method, but the system seems to take input string as null;
String save;
    public void save(){
        Actor[] allChess = GWorld.getAllObjects("Chess");  
        save="";
        if(allChess!=null){
            for (int i=0; i<allChess.length; i++){
                Chess ch = (Chess) allChess[i];
                save = save + ch.getX() +" "+ch.getY() +" "+ch.getColor()+" "+ch.getRank()+" "+"Chess";
                save =save  +"\n";
            }
        }
        System.out.println(save);
        this.save = save;
    }

    public void load(){
        
        while (save.indexOf("\n")>0){
            Chess ch;
            int x = Integer.valueOf(save.substring(0, save.indexOf(" ")));
            save = save.substring(save.indexOf(" ")+1);
            int y = Integer.valueOf(save.substring(0, save.indexOf(" ")));
            save = save.substring(save.indexOf(" ")+1);
            int c = Integer.valueOf(save.substring(0, save.indexOf(" ")));
            save = save.substring(save.indexOf(" ")+1);
            int r = Integer.valueOf(save.substring(0, save.indexOf(" ")));
            String actor = save.substring(0, save.indexOf("\n"));
            save = save.substring(save.indexOf("\n")+2);
            if ("Chess".equals(actor)&&c==0&&r==1) {
                ch = new Chess (Chess.COLOR_RED,Chess.RANK_RAT);
                GWorld.addOneObject( ch, x, y);
                ch.setImage("red_rat.png");}

        }

    }
danpost danpost

2015/11/19

#
There is a way to retain data between resets; but, I believe you really want to retain data longer, like between compilations as well. In order to accomplish that, you need to save the data to a data file and have it read back in when loading. Oh, there is another way you could have the data saved. You could use the available UserInfo storage; however, you must be able to represent the data in such a way as to not exceed the limited storage capabilities of a UserInfo data block (eleven ints and five 50-character strings).
makconas makconas

2015/11/19

#
I just want to retain data between resets, no need between compilations.
String save;
public void save(){
save = "abc" 
}// before reset
public void load(){
system.out.println(save);
}// after reset
danpost danpost

2015/11/19

#
A class field (one that is declared 'static') is not defaulted upon reset; but, is reset during re-compilation.
makconas makconas

2015/11/19

#
static can't be declared in class constructor, do you mean static string save?
danpost danpost

2015/11/19

#
makconas wrote...
static can't be declared in class constructor, do you mean static string save?
I mean to change line 1 above to:
static String save;
makconas makconas

2015/11/19

#
Sorry , but there is a problem. but it's report that string remains no changed. java.lang.NumberFormatException: For input string: " " at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:592) at java.lang.Integer.valueOf(Integer.java:766) at TestWorld.load(TestWorld.java:38)
danpost danpost

2015/11/19

#
What line is line 38 in your TestWorld class in the 'load' method? Show your revised code for the 'load' method.
makconas makconas

2015/11/19

#
it is int x = Integer.valueOf(save.substring(0, save.indexOf(" "))); I am wondering should I change into method Integer.parseInt() instead of Integer.valueOf()
makconas makconas

2015/11/19

#
do substring include empty string?
You need to login to post a reply.