Presentation is loading. Please wait.

Presentation is loading. Please wait.

Output Manipulation.

Similar presentations


Presentation on theme: "Output Manipulation."— Presentation transcript:

1 Output Manipulation

2 Manipulating Output There may come a time when outputting you may want to format the output in various ways to make it easier for readers For example, if you were creating a receipt display you may want items on one side and their values on the left. In Java we can manipulate our output in a number of ways. The first strategy is using the appropriate command, println vs. print The second strategy is using escape characters The third strategy is using text expressions

3 Escape Characters An escape character is a special character that we put INSIDE our quotes to produce a particular special output. All escape characters are preceded by a backslash, \. This is to tell the compiler that the next character following the \ has special meaning. We have 4 main escape characters in Java: \n  new line: This will force the cursor down to the next line immediately \t  tab: Inserts a tab (8 spaces) at the current cursor location \”  double quote: Our text is in quotes, this allows us to actually display a double quote in our output \\  backslash: Since \ is used to tell the compiler an escape character is coming, this allows us to include a \ in our output E.g. System.out.print(“Hello\n\tWorld!”); Outputs the following, exactly: Hello World!

4 Text Expressions An expression is something that results in a value.
You see expressions in math class all the time, e.g is an expression that results in the value 9 A Text expression is when we combine values and end up with some resulting text. E.g. “Hello” + “ “ + “World!” results in “Hello World!”

5 Text Expressions What can we combine? (Also known as concatenate)
Text and Text: “Hello” + “ “ + “World!” Text and escape characters: “Hello” + “\n” + “World!”  forces World! Down Text and numbers: “You are ” “ years old.”  “You are 16 years old.” Whoa whoa whoa…how can we combine numbers with text? Can we do this: System.out.println(“You are ” “ years old.”); Yes, we can. When Java sees that you are trying to combine text with a number it will automatically treat the number as text, e.g. 16 becomes “16” However, the number itself can be an expression that will be evaluated BEFORE the combining with the text occurs System.out.println(“You are “ + (10 + 6) + “ years old.”); will give us the same result as before Notice I wrapped the math in brackets, this is because Java will follow proper BEDMAS rules System.out.println(“You are “ “ years old.”);  Gives “You are 106 years old.” This happens because it does “You are “ + 10 before the math can occur.

6 General Expressions As shown previously, other expressions can be combined with Text expressions to give us all kinds of results. When handling mathematical expressions there are a number of operators that Java will recognize: Now, typically we will NOT do math expressions inside output statements, they will be done elsewhere and simply their result will be output to the screen. Op. Name Description Example Result + Concatenate Adds text together, or a number and text “6” + “2” “62” Add Adds numbers together 6 + 2 8 - Subtract Subtracts 2nd number from 1st 6 – 2 4 * Multiply Multiplies numbers together 6 * 2 12 / Divide Divides 1st number by 2nd 6 / 2 3 % Modulus Returns the remainder after dividing the 1st number by the 2nd 6 % 2


Download ppt "Output Manipulation."

Similar presentations


Ads by Google