Printing to Console
Sometimes the game or application might not work the way you expect it to or you want to write something to the console for logging purposes. Outside of debugging the application, you can use
System.out.println() to write your message. Lookout for your messages in the “logcat” window at the bottom of Android Studio:
- Without removing the quotation marks, try changing
"Restarting the Tac-Tic-Toe Game!"to
"Restarting the Tic-Tac-Toe Game!"or any other sentence you like. Press run to see if anything changes.
- Add another
System.out.printlnbelow the current line to print a second sentence below the first sentence.
- Press run to see if two sentences are printed out. If you see any red text, please ask for help.
HELP! I got a lot of red text!
If you see any red text, you have encountered some errors! Please ask for help. Beware of the following when coding in Android:
printlnstart with a lowercase letter.
Systemstarts with an uppercase letter.
- Make sure the sentence you wish to print is surrounded in quotation marks, and that the sentence is between the parentheses.
- Do not delete any curly braces
System.out.println is helpful when you want to fix bugs in code, but your user (the person using the app) can’t see text printed with this function. But we can do this with toasts! A toast is a quick, small text that we make visible to the user. Here’s how to make a toast:
Toast.makeText(this, "Text we want to show", Toast.LENGTH_SHORT).show()