Function return value for Main Learn programming Java

Lesson:

Functions


Exercise:

Function return value for Main 42


Objetive:

Create a java program in which you write a title (using the previous WriteTitle function) which the user will specify in command line. If no text is specified, your program will display an error message and return a value of 1 to the operatiing system.


Code:

public class Main
{
	public static void WriteTitle(String text)
	{
		int numOfSpaces = (80 - text.length() * 2) / 2;
		text = text.toUpperCase();

		// Upper line
		for (int i = 0; i < numOfSpaces; i++)
		{
			System.out.print(" ");
		}
		for (int i = 0; i < text.length() * 2 - 1; i++)
		{
			System.out.print("-");
		}
		System.out.println();

		// Real title
		for (int i = 0; i < numOfSpaces; i++)
		{
			System.out.print(" ");
		}
		for (int i = 0; i < text.length(); i++)
		{
			System.out.print(text.charAt(i) + " ");
		}
		System.out.println();

		// Lower line
		for (int i = 0; i < numOfSpaces; i++)
		{
			System.out.print(" ");
		}
		for (int i = 0; i < text.length() * 2 - 1; i++)
		{
			System.out.print("-");
		}
		System.out.println();
	}

	public static int main(String[] args)
	{
		if (args.length != 1)
		{
			System.out.println("What??!!");
			return 1;
		}
		WriteTitle(args[0]);
		return 0;
	}
}