Articles tagged with: program
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 »
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 …
Java Codes »
If you want to change the location to where the standard output messages go there’s a method in the System class called setOut() which takes a PrintStream instance as argument.In this code example we show how to redirect all standard output messages to a file by creating a FileOutputStream instance and pass it to the PrintStream instance that is sent to the setOut method, like so:
import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintStream;/** * Main.java * * @author www.javabout.com */public class Main { /** * Redirects System.out and sends all data to a file. * */ public void redirectSystemOut() { try { System.setOut(new PrintStream(new FileOutputStream(“system_out.txt”))); } …
Java Codes »
To display the number of processors available to a certain Java Virtual Machine there is a method of the Runtime class called availableProcessors().To get an instance of the Runtime class we have to call the static method getRuntime(). The example below illustrates how to go about displaying the number of processors.
/** * Main.java * * @author www.javabout.com */public class Main { /** * Displays the number of processors available in the Java Virtual Machine */ public void displayAvailableProcessors() { Runtime runtime = Runtime.getRuntime(); int nrOfProcessors = runtime.availableProcessors(); System.out.println(“Number of processors available to the Java Virtual Machine: “ nrOfProcessors); } /** * Starts …
Java Codes »
This example shows how to create a singleton object. A singleton is a class of which there can only be one instance in the same Java Virtual Machine.To create a singleton there has to be a private constructor because the class will itself control the one and only instance that will be created, and of course a private constructor cannot be called from outside the class.Instead, a method is created with public access that returns the singleton instance (if the method is called the first time the object is instantiated). …
Java Codes »
When comparing strings in Java you can either use the compareTo() method which the String class implements through the Comparable interface, or the equals() method which is declared in the Object class.Since the String class is derived from the Object class (like every class is) any of the methods can be used when working with strings in Java.In this code example we use the equals() method. Note that the method is case sensitive so if you want to make a case insensitive comparison you’ll need to use the method equalsIgnoreCase().
/** * …
Java Codes »
To replace a character in a string with another character you need to call the method replace() with the two characters as arguments.The first argument is the character to be replaced and the second is the character that will be inserted.
/** * Main.java * * @a*#117;*#116;ho*#114; www*#46;ja*#118;*#97;*#100;*#98;.*#99;o*#109; */public class Main { /** * This method replaces characters in a string */ public void replaceCharacters() { String str = “aaa bbb ccc”; str = str.replace(‘b’, ‘d’); System.out.println(str); } /** * Starts the program * * @param args the command line arguments */ public static void main(String[] args) { new Main().replaceCharacters(); }}
When the code above is executed it will display:
aaa ddd ccc
Java Codes »
>This code example shows how to format a String to a Date object. The method convertStringToDate actually takes three arguments as integers, date, month and year, and then concatenates these values to a String date.The String date is them passed to the parse method of the SimpleDateFormat object that returns a java.util.Date object. The SimpleDateFormat class takes a String format as input to the constructor.In case the format fails a ParseException is thrown.
>import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;/** * * @auth*#111;*#114; www*#46;*#106;a*#118;adb*#46;*#99;*#111;m */public class Main { public void convertStringToDate(int date, int month, int year) { SimpleDateFormat dateFormat …
