How to parse JSON using PHP

In this example, we will learn how to parse or read a JSON object using PHP built-in function. JSON Let’s create a separate file for the JSON. Copy-paste the following line of the code and save the file as “test.json” {"a":"apple","b":"banana","c":"carrot"} PHP PHP provides two variants of json_decode to parse or read the JSON. Parse... » read more

Linear Search in PHP

The following function searches for an element $x from an array called $arr by going through each element of the array, one by one. If the element is found in the array, the function returns the index of the element, otherwise, it returns -1. Linear Search function using PHP <?php function search($arr, $x) { for($i... » read more

Convert String to Enum and Enum to string in C#

For example, we have the following enum: enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } Convert enum string Use Enum.ToString method To convert an enumeration value to String. Days days = Days.Monday; string stringEnum = days.ToString(); Console.Write(stringEnum); Convert string to enum string strString = "Monday"; Days days = (Days)Enum.Parse(typeof(Days), strString); Or to... » read more

Check if a Number is valid using Java

Below is a snippet to check if a number  is valid or not. For example, the string “1.1.1” contains an invalid number, while the string “2.1” contains a valid number. The Code public class Main { public static void main (String[]args) { int i; System.out.println("Enter a Number:"); String str = System.console().readLine (); try { Double.parseDouble(str);... » read more

Retrieve Items from a LinkedList in Java

Here is another Java snippet that can help to enhance your program by using LinkedLists. The code below will retrieve all items from a linkedList and print them one by one to the console. class Main { public static void main(String[] args) { java.util.LinkedList<String> employees = new java.util.LinkedList<String>(); employees.add("Scott"); employees.add("Marie"); employees.add("Dun"); for (String string :... » read more

StringBuilder class constructors in Java

Below is a program that demonstrates the most used StringBuilder constructors in Java. public class Main { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(5); StringBuilder sb3 = new StringBuilder("Hello World"); System.out.printf("StringBuilder I = \"%s\"\n", sb1); System.out.printf("StringBuilder II = \"%s\"\n", sb2); System.out.printf("StringBuilder III = \"%s\"\n", sb3); }... » read more

Calculate the average of array element using Java

Below is a program that calculates the average value of an array using Java. public static void main(String[] args) { int[] arr= new int[]{1,2,3,4,5}; int sum = 0; for(int count=0; count < arr.length ; count++) sum = sum + arr[count]; double average = sum / arr.length; System.out.println("The average value of the array elements is :... » read more

Get the sum of array element using Java

Here is a program in Java that sums up all array element and print out the result to the console. class Main { public static void main(String[] args) { int[] arr = {1,2,3,4,5}; int total = 0; for (int i = 0; i < arr.length; i++) total += arr[i]; System.out.printf("The sum of array elements is:... » read more

Get List of Installed Windows Services using C#

Below is a program that will return all installed Windows Services on a local machine. The program uses the namespace System.ServiceProcess, which can be used by adding the Assemblies System.ServiceProcess.dll static void Main(string[] args) { System.ServiceProcess.ServiceController[] services = System.ServiceProcess.ServiceController.GetServices(); foreach (System.ServiceProcess.ServiceController service in services) { Console.WriteLine(service.ServiceName); } Console.ReadKey(); } Happy Coding! More C# Snippets

Get the square root of a number using Java

Square root can be easily obtained in Java by using the Math.sqrt function. The Math is a class in Java that contains methods that allows you to perform mathematical operations. System.out.println(Math.sqrt(9)); Result 3 More Java Snippets

Create a new Thread Delegate using C#

Below is a code that demonstrates how to create a Thread Delegate, which calls a function that will do some work for you in the background. static void Main(string[] args) { // Creating a thread Delegate System.Threading.Thread _thread = new System.Threading.Thread(new System.Threading.ThreadStart(FuctionDoBackgroundWork)); _thread.Start(); } // Fucntion that the thread delegate Starts public static void FuctionDoBackgroundWork()... » read more