About the author:

All posts by Terence:

Java's Scanner.nextLine() is being skipped

Java’s Scanner nextLine() call is being skipped (i.e. not waiting for input)

As a programming language with its fair share of quirks, one of the many things a new Java programmer will run into is the issue of their Scanner.nextLine() calls being ignored. Consider the following Java code:

JavaNextLineProblem.java

import java.util.Scanner;
public class JavaNextLineProblem {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		// Prompt the user to enter their name.
		System.out.print("Enter your name: ");
		String name = input.nextLine();

		// Prompt the user to enter their age.
		System.out.print("Enter your age: ");
		int age = input.nextInt();

		// Prompt the user to enter a description.
		System.out.print("Describe yourself in a sentence: ");
		String description = input.nextLine();

		// Prompt the user to enter a message.
		System.out.print("Enter a message: ");
		String message = input.nextLine();
	}
}

This is the desired result (user input coloured in green):

Enter your name: John
Enter your age: 21
Describe yourself in a sentence: I am awesome.
Enter a message: Hello world!

However, this is what you actually get:

Enter your name: John
Enter your age: 21
Describe yourself in a sentence: Enter a message: Hello world!

The program skips over the collection of input for the Describe yourself in a sentence prompt, and goes straight into collecting the input for the Enter a message prompt. What’s going on?

Continue reading
What to do when gitignore doesn't work

How to fix .gitignore not working on your repository

If you’re using a Git-based source control, you might be familiar with the use of a .gitignore file to list files which Git is supposed to ignore when tracking files and making commits. This can be particularly useful when using Git (if you’re using GitHub Desktop, you’re also using Git) as a source control tool for projects that generate temporary or user-specific files at every run, such as Unity, as these files cannot be shared across different users of the project.

User-generated files in Unity
User-generated files in Unity that don’t need to be tracked by Git.

Sometimes, when setting up the repository, because of a .gitignore that is not properly set up, files that you intend to be ignored can actually get committed into the repository. If that happens, retroactively applying the .gitignore list will not help.

Continue reading
For security reasons, this URL is only accessible using localhost (127.0.0.1) as the hostname.

Bitnami phpMyAdmin: For security reasons, this URL is only accessible using localhost

If you’re running a Bitnami LAMP stack for your web server, you will be glad to find that it comes with phpMyAdmin — an indispensible tool for managing your server’s SQL server. If you try to access said SQL server (the default URL is at yourdomain.com/phpmyadmin), however, you will find an error message that goes like this:

Continue reading
A primer on Base 64 strings — Part 1

A primer on Base 64 strings — Part 1: Introduction

If you have been around the web for awhile, you will notice that sometimes, you will find a series of gibberish alphabets appearing, most often in your browser address bar as part of a website’s URL:

The blue highlighted portions in the GIF above are the aforementioned gibberish alphabets.

These are Base 64 characters, and a large portion of the web uses these characters for a multitude of purposes.

Continue reading
Finding which is the shorter 2D angle

Calculating the shorter angle of rotation in 2D

Here’s a really simple math problem in 2D games development that has a surprisingly complex solution. If I were to give you angles a and b, how would you calculate 1) the direction — that is, clockwise being positive; and anti-clockwise being negative — and 2) magnitude of the shorter angle of rotation from a to b?

Main angle question
We want the angle in green.

When you visualise it, both values seem so obvious; which was why I was so surprised I couldn’t figure it out. On the surface, it seems really simple — if you just take b – a, doesn’t it give you the solution?

Continue reading
Static variables explained, in Java

Static variables, explained (using Java)

One of the more complicated concepts that beginning programmers have trouble grasping is static variables. This is in part because static variables work in a way that is quite opposite to how classes and Object-Oriented Programming (OOP) work.

Since examples work wonders in explaining concepts, here’s a use-case in Java that will help to illustrate the concept. Don’t code in Java? No problem. OOP concepts are (mostly) the same across programming languages.

Continue reading
Fixing off-centre Tiles in the Unity Tile Palette

Fixing an off-centre Tile Palette in Unity

When importing Tiles into the Tile Palette in Unity, you may sometimes run into an issue where your Tiles are not aligned with the grids in the Tile Palette, which will subsequently lead to the Tiles being off-alignment with the grid in your Scene too.

Continue reading
Setting up Unity's Device Simulator

Setting up Unity’s Device Simulator

Unity’s Device Simulator is a very nice extension in Unity that allows users to quickly and easily test out their games on a mobile platform. It extends the Game window by adding a dropdown that allows the user to switch between the Game view and a newly-added Simulator view.

Continue reading