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

2014/4/4

How do you ask a user to type in his/her name in Greenfoot?

BeYouself BeYouself

2014/4/4

#
I need help with UserInfo and asking the user to type in his/her name. Could anyone type in the code?
danpost danpost

2014/4/4

#
If you are using UserInfo, then the user must be logged in and the program can acquire the username through the UserInfo object created for that user.
String userName;
if (UserInfo.isStorageAvailable());
{
    UserInfo myInfo = UserInfo.getMyInfo();
    userName = myInfo.getUserName();
}
BeYouself BeYouself

2014/5/13

#
Could you type in the whole code because i am very new to UserInfo
danpost danpost

2014/5/13

#
That is the whole code, but if you want it in method form, it would be something like this:
public String getNameOfUser()
{
    String username;
    if (UserInfo.isStorageAvailable())
    {
        UserInfo myInfo = UserInfo.getMyInfo();
        return myInfo.getUserName();
    }
    return "";
}
This method will return an empty String, or a String object of zero length, ( "" ) if the user is not logged in.
You need to login to post a reply.