While you can use encoders to drive straight, a better idea might be to use a gyro since encoders can have errors due to wheels slipping. Regardless of the sensor you choose, you will want to use a PID controller to help correct the drift. When using encoders, one method would be to use a master-slave setup as described in
this article. The example code is in ROBOTC, but the same principles apply. Basically you measure the speed difference between the two sides of the drivetrain and use that error with your PID controller to correct the drift.
Searching the forums will also provide you with more information on this topic, such as
this thread.