Java How To Sort an Array

How To Sort an Array

You can use the sort() method, found in java.util.Arrays, to sort an array:

Example

import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    String[] cars = {"Volvo", "BMW", "Tesla", "Ford", "Fiat", "Mazda", "Audi"};
    Arrays.sort(cars);
    for (String i : cars) {
      System.out.println(i);
    }
  }
}

Continue reading Java How To Sort an Array

Java How To Convert a String to an Array

Convert a String to an Array

There are many ways to convert a string to an array. The simplest way is to use the toCharArray() method:

Example

Convert a string to a char array:

// Create a string
String myStr = "Hello";

// Convert the string to a char array
char[] myArray = myStr.toCharArray();

// Print the first element of the array
System.out.println(myArray[0]);

You can also loop through the array to print all array elements:

Example

// Create a string
String myStr = "Hello";

// Convert the string to a char array
char[] myArray = myStr.toCharArray();

// Print array elements
for (char i : myArray) {
  System.out.println(i);
}

Continue reading Java How To Convert a String to an Array

Java How To Calculate the Sum of Elements

Calculate the Sum of an Array

Get the sum of array elements:

Example

int[] myArray = {1, 5, 10, 25};
int sum = 0;
int i; 

// Loop through the array elements and store the sum in the sum variable
for (i = 0; i < myArray.length; i++) {
  sum += myArray[i];
}

System.out.println("The sum is: " + sum);

Continue reading Java How To Calculate the Sum of Elements

Java How To Reverse a String

Reverse a String

You can easily reverse a string by characters with the following example:

Example

String originalStr = "Hello";
String reversedStr = "";

for (int i = 0; i < originalStr.length(); i++) {
  reversedStr = originalStr.charAt(i) + reversedStr;
}

System.out.println("Reversed string: "+ reversedStr);

Continue reading Java How To Reverse a String

Java How To Count Words

Count Number of Words in a String

You can easily count the number of words in a string with the following example:

Example

String words = "One Two Three Four";
int countWords = words.split("\\s").length;
System.out.println(countWords);

Continue reading Java How To Count Words

Java How To Add Two Numbers

Add Two Numbers

Learn how to add two numbers in Java:

Example

int x = 5;
int y = 6;
int sum = x + y;
System.out.println(sum); // Print the sum of x + y

Continue reading Java How To Add Two Numbers

Java Delete Files

Delete a File

To delete a file in Java, use the delete() method:

Example

import java.io.File;  // Import the File class

public class DeleteFile {
  public static void main(String[] args) { 
    File myObj = new File("filename.txt"); 
    if (myObj.delete()) { 
      System.out.println("Deleted the file: " + myObj.getName());
    } else {
      System.out.println("Failed to delete the file.");
    } 
  } 
}

The output will be:

Deleted the file: filename.txt

Continue reading Java Delete Files

Java Create and Write To Files

Create a File

To create a file in Java, you can use the createNewFile() method. This method returns a boolean value: true if the file was successfully created, and false if the file already exists. Note that the method is enclosed in a try...catch block. This is necessary because it throws an IOException if an error occurs (if the file cannot be created for some reason): Continue reading Java Create and Write To Files

Java Files

File handling is an important part of any application.

Java has several methods for creating, reading, updating, and deleting files.


Java File Handling

The File class from the java.io package, allows us to work with files.

To use the File class, create an object of the class, and specify the filename or directory name: Continue reading Java Files