Tuesday, October 14, 2008

Getting Started

As I walked 'round the class today while you were working on the AccountSetup program, I noticed more than a couple of you have trouble just getting started. Once you get over that initial hump, you seem to be able to make progress. But the first step's a doozy.

So, how to get started? How, for that matter, do I get started?

It's a hard process to describe and I suspect that I don't always proceed in the same way.

The first thing I do when getting started is to pick something simple that the program asks me to do. In AccountSetup (p. 139) one of the things we're asked to do is to get a user name and later on to display it. So I'd do that first. I'd declare a variable to hold the user name and then prompt the user to enter a user name and since I'll have to display it later, I might as well as display it now.

String userName;

System.out.print("Enter a user name, please: ");
userName = System.Console.ReadLine();

System.out.println("Your user name is " + userName);

OK. That's a start. And it also relieves you of some of the mental effort of keeping track of that particular part of the puzzle.

The other thing we had to do in AccountSetup was to prompt the user for a password and keep prompting them until they entered a password with at least 8 characters. Of course, this means that we need a loop (because we're going to keep prompting them until we get a password that 8 characters or more long). It also means that you'll need another String variable to store the password in. This part of the program can be implemented with either a while or a do...while loop. And you might as well make the condition that keeps the loop running the thing that checks to see if the password is 8 characters long or not.
do
{
System.out.print("Enter a password (8 characters minimum): ");
password = System.Console.ReadLine();
} while (password.length < 8);

I'll leave the last part up to you. That's the output part. Don't forget to convert the user name and password to lower case!!

No comments: