Java Examples Explained

Famous Quotes

Follow Java Examples Explained on WordPress.com

Categories

Let’s say we want to display a famous quote by Robert Frost in the following way:


"Two roads diverged in a wood, and I --
 I took the one less traveled by, 
 and that has made all the difference."
				~Robert Frost

There are two things we want to achieve here, display the double quotes (“), and display Robert Frost to the far right.

The program below shows one way to do it:

1|public class FrostQuote { 
2|   public static void main(String[] args) { 
3|      System.out.print("\"Two roads diverged in a wood, ");
4|	System.out.println("and I --");
5|	System.out.print(" I took the one less traveled by, \n");
6|	System.out.println(" and that has made all the difference.\"");
7|	System.out.println("\t\t\t\t~Robert Frost");
8|   } 
9|}
	

On the first line of Robert Frost’s quote, we have a (“) before the word ‘Two’. In order to display this symbol, we have to add a backslash symbol (\) as shown on line 3. Why? Remember that println() method will print anything you put within two (“)s. So the first (“) will mark the beginning of the sentence you want to print, and it will print anything until it finds the next (“) that marks the end of the sentence. So, if on line 3, you don’t add the (\), the (“) you add before the word ‘Two’ will be assumed as the mark of end of sentence. And the rest of the sentence after the (“) does not follow the proper format of println() method, therefore will produce an error. By adding the (\), it differentiates the following (“) from the (“) marking the end of sentence, and therefore will be printed as it is. We use the same symbol on line 6 to print the (“) after end of Robert Frost’s quote.

Now, we look at how we make Robert Frost printed further to the right of the screen. If we are using a word processor, what we do is just press the tab key a few times. Well, here we use similar concept actually, by using a control character that have the same effect as pressing a tab key, which is backslash and ‘t’ (\t). As you can see on line 7, we use (\t) 4 times to mimic the effect of pressing tab keys 4 times.

You may notice that in this example we use two methods to display our sentences, print() and println(). What is the difference between the two? As you might have guessed, println() method will display the sentence between two (“) on one line, and then move the cursor to the next line and ready for the next instruction. On the other hand, print() method will stay on the same line after displaying the sentence. That is why line 3 and 4 of the code produce the first line of Robert Frost’s quote.

What about the print() method on line 5? Why do you think it has the same effect as using println() method? Because in there, we have another control character, backslash and ‘n’ (\n), which is also known as newline character. It has the same effect as pressing return or enter key, which moves the cursor to a new line. So even though print() does not move your cursor to the next line, the newline character (\n) does.

There are a few other control characters that allow you display your sentences the way you like it, but the most popular ones are (\n) and (\t).

The previous example and this example show us how to display sentences using print() and println(). In order to change the sentences, we have to modify the program, recompile and run it again. Otherwise, every time we run the program, it will produce the same output. So, how do we make our program more flexible, and allow us change certain things without modifying the program and recompiling it? The next example, Customised Hello will show you how.

You could also have a look at the GUI version of this program at Famous Quotes (GUI)‎.


Leave a comment