3

I am developing a console based application.

Is it a good idea to use System.out.println() to display error messages on the console? I am using a lot of these in my classes for error handling.

My class looks something like this:

public void validatePlayerSelection(String playerSelection, List<String> listOfIndex) {
    try {
        if (playerSelection == null || playerSelection.isEmpty()) {
            throw new NullPointerException();
        } else if (Integer.parseInt(playerSelection) < 11
                || Integer.parseInt(playerSelection) > ((size * 10) + size)) {
            System.out.println("The input you have entered cannot be found on the board");
        } else if (listOfIndex.contains(playerSelection)) {
            System.out.println("Cell already taken. Please select a different move.");
        } else {
            flag = false;
        }

    } catch (NumberFormatException e) {
        System.out.println("The input must be a number on the board");
    }
}

What is the alternative to System.out.println? Also, is this an efficient way to write code?

  • I'm pretty sure you can also use `System.err` for errors. For console applications these messages will generally show up in the same place. Also see [this answer](https://stackoverflow.com/a/3385261/7470253) – M. Prokhorov Apr 12 '18 at 11:04

1 Answers1

3

No. You use the console for small things, like: experimental code.

As soon as we are talking anything to be used in the real world, you look out for using some sort of Java logging framework.

For example log4j, or using the built-in logging facilities.

Seriously, even when you just create your own little command line helper tools: learn how to use a logging framework, and use it. You want for example to be able to emit messages dependent on some sort of logging level (think: verbose, info, warnings, errors). If you not use an existing framework, you will inevitably start to create your own framework at some point. Repeating all the mistakes than you can make while doing so, for no reason at all (besides the learning experience).

Also understand: logging isn't the same as "user interaction". All the things your example does are about getting information from the user (or giving him some responses/feedback). For that, you use the most appropriate path (which could be system.out). But for anything "else" that your application might want to "log" - you better look out for alternatives than system.out resp. system.err.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • But it is a simple console application. So won't System.err or System.out be efficient to display error messages to the user? – perpetualcoder Apr 12 '18 at 10:51
  • @Vishakha Many many many things started "as a simple thing". Therefore people started off with something insufficient. Thing is: *anything* that is useful (for you or others) tends to **grow**. And then, after you invested a lot of time, you find: "it is just such a pain how this things logs information" - but then you will have to make changes to in zillions of places. Again: *any* java developer should know how to do proper logging. You have to learn it anyway. So learn it now, and practice using it. – GhostCat Apr 12 '18 at 10:55
  • I don't think log is the place where program results are stored. Abstracted output is one thing, but only having output to logs is another one entirely. – M. Prokhorov Apr 12 '18 at 10:57
  • @M.Prokhorov True, I updated my answer a bit to better fit that distinction. – GhostCat Apr 12 '18 at 11:00