Java Programming Tips


Writing a Text File in Java

Java has plenty of classes for file manipulation. For simple text files which contains lines of text, the best classes to use are FileWriter and PrintWriter in java.io package. PrintWriter has the method println() which writes a string into the file followed by a new line. Also note that PrintWriter and FileWriter must be closed […]

Splitting Strings in Java

One of the common string processing requirements in computer programs is to split a string into sub strings based on a delimiter. This can be easily achieved in Java using its rich String API. The split function in String class takes the delimiter as a parameter expressed in regular expression form. The following Java program […]

How to Print Asterisk Triangle Pyramid in Java

Printing asterisk triangle is common programming problem given to beginners learning Java language. This will help students to grasp the power of loop programming construct. There are a number of variants of this problem. The following example shows how a triangle pyramid can be printed using Java. * *** ***** ******* ********* *********** ************* *************** […]

How to Multiply Two Numbers in Java

Simple programming problems such as multiplication of numbers is a good way to teach programming syntax without students getting overwhelmed by the programming solution. The following Java program demonstrates multiplication of two numbers. This is a trivial example program, however it shows how to handle input and output, performing calculations and the use of library […]

How to Check If a Triangle Can be Formed in Java

The sides of a triangle is constrained by the rule that the sum of any of its two sides is always bigger than the third side. Using this rule, we can find whether 3 sides given for a triangle is valid or not. The following sample Java program checks whether the given 3 sides can […]