Sunday 18 December 2011

Java: String Formatting (Pictures!) and the Switch Statement

Okay so, apparently no pictures is a huge no-no, so I'll briefly show you how string formatting works with the power of screenshots! And yeah, looking back on yesterday's post, I agree, it was kinda shitty, so, sorry about that :P

So, string formatting! First, we need to formulate a template string in which data will be entered later!

Then we need to somehow calculate the data to put in, same as you would with any other output that takes a variable, and finally, we format the string! Let me show you how:


Okay, Line 114 is our template. As you can see, there are 3 percentage signs followed by a letter, so the template has 3 spaces for data in it, The "%s" denotes a string, and "%d" denotes a decimal integer!

Note: If you want to print a percent sign, you must place 2 of them in a row so the computer doesn't read is as trying to place a formatted spot in

Our data can be collected however, a prompt from the user, or a method that performs some calculation. For the sake of ease, on lines 116-118 I've just created the Strings and integer directly! Sure, I could have put both names into 1 string, but then I don't get to illustrate what happens when you have 2 of a data type in the strong.

Next, we want to call the String.format method on our string (Lines 120-121), taking 1 parameter plus the number of formatted spots. In our case, 3 formatted spots means 4 parameters! Each parameter after the first corresponds to a formatted position, in order of appearance.
So FIRST_NAME takes the spot of the first %s, LAST_NAME takes the spot of the second %s and USER_AGE takes the spot of the 3rd formatted space, which is a %d.
Note: If you have the wrong data type in place (Say I mixed up USER_AGE and FIRST_NAME, heads will roll, and programs will crash!

String.format returns the string given, but fully formatted, so either print it directly, or save it to a variable, as I have, then print it as in line 123.

At the bottom of the screenshot, in the black box, is the output, if you needed to visualize that, too! 
Much better than typing "System.out.println( "My name is" + FIRST_NAME + " " + LAST_NAME +..." don't you think?
I really hope that was a sufficient explanation this time! Let me know if you're still having trouble!



Second half of today's post: A quick refresher on switch statements! Okay, so they're basically if-statements for larger groups, where typing else if {... a bunch of times can get really annoying! The general way this works is having a switch as the opening for a block of statements, followed by a bunch of case keywords, like so...

Here, I have a method that takes an int that's composed entirely of a switch-statement, on Line 16 we can see the declaration which takes some value as it's argument. This value doesn't need to be an integer, it can be anything, as long as the cases correspond to it!

For each position that a special message appears for, I create a case (Lines 17, 20 23), each followed by the value of position that they're triggered on. Inside each case block is a statement (or sequence of statements) to be executed in that case, followed by a break to leave the switch (If you already determine position to be "1", why go through all the other cases?).

If the position is 1, print out first place, if 2, print second and so on. Not necessary, but often used, is the default keyword. This is invoked if none of the cases match the switch-variable! In our case, if you're not of the top 3, you're a damn loser! Got it? Let's test it out now!

The second part of our picture is a simple for-loop, going through 0-4 and calling our method for each value. The final part of the picture shows the integer put into the method followed by the output!

I had pictures, that means I was coherent this time, right? Right? Let me know if there are still any troubles though, by any means! All questions and comments welcome, see you tomorrow for an actual switch statement in action!

6 comments:

  1. Yeah, photos help, or at least visuals or something. Hehehe

    ReplyDelete
  2. Well this post certainly made up for the last one! I appreciate the time you put into this.

    ReplyDelete
  3. It's easier with photos. (:

    ReplyDelete
  4. It actually took me almost an hour to gather everything up nicely, glad it shows! Probably wouldn't have been needed if I wasn't being lazy with the last one, though!

    ReplyDelete
  5. But what if your age is James and your name is 21 Heddin?

    ReplyDelete