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

2012/7/1

Bug saving in UserInfo

SPower SPower

2012/7/1

#
I want to add banning to my Save it scenario, so you have to wait 1 day before you can access your account again if you typed a wrong password 3 times. This is my code to save the date of banning in UserInfo:
    private static final int BANNING_INDEX = 2;
[...]
    public void banUser()
    {
        if (this.info != null) {
            long currentTime = System.currentTimeMillis();
            Long object = new Long(currentTime);
            this.info.setString(BANNING_INDEX, object.toString());
        }
    }
    
    public boolean isUserBanned()
    {
        if (this.info != null) {
            String string = this.info.getString(BANNING_INDEX);
            System.out.print("in isUserBanned: " + string + "\n");
            Long beginObject = Long.getLong(string);
            if (beginObject == null) {
                return false;
            }
            long banBegin = beginObject.longValue();
            long day = 1000*60*60*24;
            long currentTime = System.currentTimeMillis();
            if (banBegin + day <= currentTime) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
The only problem is that the string 'string' is empty, which means that 'beginObject' is null, and it will always return false. I don't know what I did wrong, so if somebody can help, thank you!
erdelf erdelf

2012/7/1

#
I think the following could work, it should be in public:
if (this.info != null) 
{  
        String string = this.info.getString(BANNING_INDEX);  
        Long beginObject = Long.getLong(string);  
        long day = 1000*60*60*24;  
        long currentTime = System.currentTimeMillis();  
        if (beginObject == null) 
        {  
            this.info.setString(BANNING_INDEX, currentTime - day);
        } 
} 
SPower SPower

2012/7/1

#
That won't be what I want: this line is wrong:
this.info.setString(BANNING_INDEX, currentTime - day); 
because it's not always 1 day ago when the user was banned.
erdelf erdelf

2012/7/1

#
It was just an idea how to fix your problem with the empty String. If there is no string, the code should fill the string.
SPower SPower

2012/7/1

#
But the string is always "", even if I called banUser before....
erdelf erdelf

2012/7/1

#
well, I can't see why banUser shouldn't write a string.
SPower SPower

2012/7/1

#
Who do you think I posted it on greenfoot.org? I really have no idea what's wrong..
erdelf erdelf

2012/7/1

#
did you looked in the storage.csv?
SPower SPower

2012/7/1

#
Yes, empty (for that string).
SPower SPower

2012/7/1

#
Can somebody else help me? I've got no idea what's wrong...
danpost danpost

2012/7/1

#
You could simplify lines 6 through 8 with this:
this.info.setString(BANNING_INDEX, "" + System.currentTimeMillis());
And, I am not sure about line 18. Try inserting the following before line 17, and remove lines 18 through 20:
if (!"".equals(string)) return false;
SPower SPower

2012/7/2

#
I maybe have to explain it a little bit better, because it's still not working. The problem is that the 'string' object is always empty, so it will always return false. And with this:
if (!"".equals(string)) return false;
Do you mean this:?
if ("".equals(string)) return false;
SPower SPower

2012/7/2

#
I'm sorry for causing trouble, I just forgot the store command, as I always do :). But usually I see that I forgot it, and this time, I didn't.
You need to login to post a reply.