Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

In Java, it's pretty easy to get simple input from a keyboard and output to the computer screen.  It might seem simple, but this functionality is a building block for more interesting games and interactions.  Right now, let's focus on output to start.   

Hopefully, you went through the post about running your first Java program.  If you haven't, go back and do that step, and then come back to this post.  From this point, I'm assuming that you have either set up an IDE or have been able to use a text editor and command line to run a program. 

How Do You Output Text in Java?  

To output text, we use a method called System.out.println().  We'll discuss methods very soon.  The usage looks like this:   System.out.println("Your output text here");  

Pay close attention to the case, the System part should always be capitalized.  The case does not matter for the text in between the quotation marks.  

Simply replace the code text in between the parentheses (which says "Your output text here") with whatever text you want.  Make sure that your output text is all between quotation marks and inside the parentheses, and that you end the line with a semicolon after the closing parenthesis.  

So now we can write a very simple program asking the player to press a button.  Let's look at some short example code.  Have a look at the code I posted at https://codepad.co/snippet/AR6xKxde.  If you want to run it yourself, copy and paste it into an empty file on your computer, and name the file GameIOExample.java.   

You'll notice on lines 7 and 14, we have our System.out.println("…"), and then in between the parentheses, there's quotation marks and text.  If you run the program, you'll see that everything in between those parentheses is output as text to the screen.  It's that simple to output basic text!  Try the following:  

  • Change the text in between the quotation marks, run the program, and see how the output changes.
  • Add another System.out.println() statement, after line 6 but before line 9, and put whatever text you want between the parentheses.  Don't forget to put the text between quotation marks, and don't forget to put a semicolon after the closing parenthesis.