The most common problems I see in Java amongst newbies to the language is the use of the Scanner class. This particular class was created in order to make reading in data input from the command line easier for those new to the language. However, I fear it only half worked. Scanner is still a big pain to use.
First of all, do not even ATTEMPT to mix data types with the same scanner. It will not work. Having a Scanner read in a line of text and then read a single integer will cause an exception to be thrown, and then the person first learning Java gets a big headache and hasn’t a clue about what is wrong with the code. The fix for this by the way is to simply have different Scanners work over System.in, making sure each one reads the data in only one format.
The next biggest problem with Scanner that I see is the use of the hasNext() method. You cannot use the hasNext() method effectively when reading keyboard input from the command line. Usually I see this kind of code:
while (input.hasNext()) {
System.out.println(input.next());
}
If you cannot see the problem with this while loop, it’s because you have not realized that in this case, input is a scanner looking at System.in . The only way this loop will stop is if the end of the file is reached, and well, the command line can never reach the end of the file. This means the only way to stop this loop would be to input the EOF representation into the keyboard, which most people do not know.
Scanner has its problems, and if you want to advance as a programmer, I highly suggest using a BufferedReader. It’s a little more complicated but much less of a hassle later on, especially when you begin getting into much more complicated streams of data.