Articles tagged with: java code examples
Java Codes »
This code example shows how to get a substring from a string. To do that you need to know atleast the starting index of the substring and a good way to find that out is the indexOf() method of the String class.The substring() method takes either start index (inclusive) and end index as parameters, or just the start index which will return the remainder of the string.The example below shows both ways:
/** * Main.java * * @*#97;*#117;th*#111;*#114; *#119;*#119;w*#46;*#106;*#97;*#118;*#97;*#100;b*#46;c*#111;m */public class Main { /** * Gets a substring from a string. */ public void getSubstring() { String str = “Getting …
Java Codes »
To convert all upper case characters in a string to lower case, use the method toLowerCase() of the String class.The example below shows how to use it. Note that the method returns a String so you’ll need to assign the return value of the method to a variable (in this case the same variable).
/** * Main.java * * *#64;au*#116;h*#111;r *#119;*#119;*#119;.*#106;*#97;*#118;*#97;*#100;*#98;.*#99;om */public class Main { /** * Converts all upper case characters to lower case */ public void stringToLowerCase() { String str = “All Upper Case Characters In This String Will Be Converted To Lower Case”; str = str.toLowerCase(); System.out.println(str); } /** * Starts the …
Java Codes »
Since Java 6 there is a new way to check if a String is empty. The most common way to determine if a String is empty is to call the length() method of the String class and check if it returns 0. There is now a method added to the class called isEmpty() which returns a boolean value.The output from the code below is:truetruefalsefalse
/** * * @author javabout.com */public class Main { /** * Example method for checking if a String is empty */ public void checkEmptyString() { String a1 = “”; String b1 = “not an empty string”; System.out.println(“Checking …
Java Codes »
When a Java program has gone through all code it exits automatically by returning zero if all has gone well.In some situations you might want to exit the program with a line of code, and this is done by calling the static exit method of the System class.The exit method takes an int as argument so if you want to exit the program normally you write:
System.exit(0);
Should you want to exit because of for example some error has occured you normally specify a negative number as the argument.You could of course …
Java Codes »
By listing the properties of the system you run the program on you can find out a lot about it. Your program maybe should act in a certain way depending on what operating system it’s currently on.This code example shows how to retrieve the properties and how to enumerate through and print them out to the console.
import java.util.Enumeration;import java.util.Properties;/** * * @author javabout.com */public class Main { /** * Lists the system properties */ public void listSystemProperties() { Properties prop = System.getProperties(); Enumeration enumeration = prop.keys(); while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); System.out.println(key ” = “ …
Java Codes »
There are severals ways to accomplish this. The example below shows two of them.The second example is only possible if you run Java version 1.5 or later.
import java.text.DecimalFormat;public class NumberUtil { public void roundNumber(double numberToRound) { DecimalFormat df = new DecimalFormat(“0.000″) ; System.out.println( “Rounded number = “ df.format( numberToRound ) ) ; //This only works with Java runtime 1.5 or later System.out.println(String.format(“Rounded number = %.3f”, numberToRound)); } public static void main(String[] args) { NumberUtil util = new NumberUtil(); util.roundNumber(1.23856); }}
The output of either one of the above rounding methods is:“Rounded number = 1,239″
Java Codes »
Since strings are immutable, you may want to use the StringBuilder class if you’re going to alter the String in the code. The StringBuilder class can be seen as a mutable String object which allocates more memory (or deallocates if told so by using the trimToSize() method) when it’s content is altered.It should be said early that the StringBuilder class is not thread-safe which means that it doesn’t automatically handle synchronization of access to the object by different threads as opposed to the StringBuffer class.If this object needs to be …
Java Codes »
This code snippet shows how to find out the minimum and maximum values of the byte datatype.To get the minimum value you simply call the static variable MIN_VALUE of the Byte class, and MAX_VALUE for the maximum value.
/* * Main.java * * @author www.javabout.com */public class Main { /* * This method displays the minimum and maximum values of the byte datatype. */ public void displayMinAndMaxValuesOfByte() { System.out.println(Byte.MIN_VALUE); System.out.println(Byte.MAX_VALUE); } /** * @param args the command line arguments */ public static void main(String[] args) { new Main().displayMinAndMaxValuesOfByte(); }}
The min and max values of a byte are:-128127
Java Codes »
The class ‘Float’ is a wrapper class for the datatype float, and it has two static variables that contains the minimum and maximum values of the float datatype: MIN_VALUE and MAX_VALUE.You can print out the min and max values of the float datatype by specifying these variables as arguments to System.out.
/* * Main.java * * @author www.javabout.com */public class Main { /* * This method displays the minimum and maximum values of the datatype float. */ public void displayMinAndMaxValuesOfFloat() { System.out.println(Float.MIN_VALUE); System.out.println(Float.MAX_VALUE); } /** * @param args the command line arguments */ public static void main(String[] args) { new Main().displayMinAndMaxValuesOfFloat(); }}
The output from the executed code will …
Java Codes »
Often it is necessary to validate input data. One such validation could be to check if a user has entered a sequence of numbers.This example shows how you can check a String to see if it only contains integers. This validation could be done with less code if we were using regular expressions, but this example aims to keep it simple and more understandable so we use the Character wrapper class instead.The Character class has a static method called isDigit() which takes a char value as argument and returns true …
