Best practice way to read input in Java 5 Best practice way to read input in Java 5 unix unix

Best practice way to read input in Java 5


Scanner is pretty basic and less code.

import java.util.Scanner;class Example{    public static void main(String args[]){         Scanner s = new Scanner(System.in);         String inputString = s.nextLine();         //Output stuff    }}


I think a Scanner is a bit easier than a BufferedReader, personally. That's how I learned user input initially.

You have to do some of the same things but you can leave off the exception handling and try/catch for a later day.


Some rhetorical questions:

  • What age are the kids?

  • What are you aiming to achieve in terms of educational outcomes? Or is this mainly about giving them something fun to do.

  • What is your level of experience with Java?

My gut feeling is that you should not be going overboard with the "best practice" stuff. Assuming that these are school age kids, they don't need it, they probably don't have the outlook to understand it. So for most of them it is going to be boring blah blah, and a good way to turn them off IT.

Besides, it is difficult to teach "best practice" if you don't fully understand it yourself. The way I see it, "best practice" is about techniques that are best most of the time. To teach them properly you need to understand (and be able to explain) why they work, and when they don't work. It also helps if you understand a bit about the "not best practice" ways of doing things too ... so that you can explain the difference.

(I'd also question why you are restricting yourself to Java 5 ... but that's not particularly relevant.)


When I asked about "educational outcomes" I was mean things like:

  • student gets/stays enthusiastic about programming, or
  • student achieves such and such a level of proficiency, or
  • ...

It is one of the first questions that someone in the business of teaching should be asking themselves.

Having said that, I think I'll stick with my advice above. If you want to beat the drum about "best practice", try to structure your material so that it is easy for the average kid to skip over the "boring" bits.