Java for Complete Beginners (Video), Part 20: StringBuilder and String Formatting

How to use StringBuilder, StringBuffer and why you should use them. This video also unveils the mystery of formatting strings with printf() and related methods; vital skills for any Java course or aspiring software developer.

When the video is running, click the maximize button in the lower-right-hand corner to make it full screen.

Code for this tutorial:


public class App {


	public static void main(String[] args) {
		
		// Inefficient
		String info = "";
		
		info += "My name is Bob.";
		info += " ";
		info += "I am a builder.";
		
		System.out.println(info);
		
		// More efficient.
		StringBuilder sb = new StringBuilder("");
		
		sb.append("My name is Sue.");
		sb.append(" ");
		sb.append("I am a lion tamer.");
		
		System.out.println(sb.toString());
		
		// The same as above, but nicer ....
		
		StringBuilder s = new StringBuilder();
		
		s.append("My name is Roger.")
		.append(" ")
		.append("I am a skydiver.");
		
		System.out.println(s.toString());
		
		///// Formatting //////////////////////////////////
		
		// Outputting newlines and tabs
		System.out.print("Here is some text.\tThat was a tab.\nThat was a newline.");
		System.out.println(" More text.");
		
		// Formatting integers
		// %-10d means: output an integer in a space ten characters wide,
		// padding with space and left-aligning (%10d would right-align)
		System.out.printf("Total cost %-10d; quantity is %d\n", 5, 120);
		
		// Demo-ing integer and string formatting control sequences
		for(int i=0; i<20; i++) {
			System.out.printf("%-2d: %s\n", i, "here is some text");
		}
		
		// Formatting floating point value
		
		// Two decimal place:
		System.out.printf("Total value: %.2f\n", 5.6874);
		
		// One decimal place, left-aligned in 6-character field:
		System.out.printf("Total value: %-6.1f\n", 343.23423);
		
		// You can also use the String.format() method if you want to retrieve
		// a formatted string.
		String formatted = String.format("This is a floating-point value: %.3f", 5.12345);
		System.out.println(formatted);
		
		// Use double %% for outputting a % sign.
		System.out.printf("Giving it %d%% is physically impossible.", 100);
	}

}

My name is Bob. I am a builder.
My name is Sue. I am a lion tamer.
My name is Roger. I am a skydiver.
Here is some text.	That was a tab.
That was a newline. More text.
Total cost 5         ; quantity is 120
0 : here is some text
1 : here is some text
2 : here is some text
3 : here is some text
4 : here is some text
5 : here is some text
6 : here is some text
7 : here is some text
8 : here is some text
9 : here is some text
10: here is some text
11: here is some text
12: here is some text
13: here is some text
14: here is some text
15: here is some text
16: here is some text
17: here is some text
18: here is some text
19: here is some text
Total value: 5.69
Total value: 343.2 
This is a floating-point value: 5.123
Giving it 100% is physically impossible.

 

Learn to program Java Swing with my complete video course – desktop programming and applets. Includes 7 free videos. Click here for details.

All pages on this site are copyright © 2013 John W. Purcell

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Posted in Java, Java Video Tutorials (Beginners) | Tagged , , , , , |