How to learn Java in 1 day

Learning Java in one day is not a realistic goal, as Java is a complex programming language that requires time and practice to master. However, here are some tips to get you started: Start with the basics: Before diving into complex topics, learn the basics of Java, such as variables, data types, control structures, and object-oriented programming principles. Watch tutorials and read documentation: There are many resources available online that can help you learn Java quickly....

March 24, 2023 · 1 min · 195 words · Andrew

How to Find the nth Reverse Number in Java

The challenge Reverse Number is a number which is the same when reversed. For example, the first 20 Reverse Numbers are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101 Task You need to return the nth reverse number. (Assume that reverse numbers start from 0 as shown in the example.) Notes 1 < n <= 100000000000 The solution in Java Option 1:...

December 12, 2022 · 2 min · 263 words · Andrew

How to Find the Sum of Intervals in Java

The challenge Write a function called sumIntervals/sum_intervals() that accepts an array of intervals, and returns the sum of all the interval lengths. Overlapping intervals should only be counted once. Intervals Intervals are represented by a pair of integers in the form of an array. The first value of the interval will always be less than the second value. Interval example: [1, 5] is an interval from 1 to 5. The length of this interval is 4....

December 11, 2022 · 3 min · 601 words · Andrew

How to Compile Multiple Java Files from a Single Command in Java

If you need to compile multiple Java files using a single command, then you can do the following. First, it’s good to learn how to compile a Java file. How to Compile a Java File Let’s take the following Java code: class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } To compile this, we simply do the following: javac HelloWorld.java Then when we need to run it, we do:...

October 12, 2022 · 1 min · 124 words · Andrew

How to Convert JSON to a Java Object

If you need to convert JSON to a Java Object, then you can do one of the following: Option 1 – Using Gson import com.google.gson.Gson; public class SimpleTesting { public static void main(String[] args) throws InterruptedException { String json = """ { "firstName" : "Jane", "lastName" : "Doe", "dateOfBirth" : "1973-04-29", "address" : "81 Hype", "city" : "New York", "contact" : "0123456789" } """; Student data = new Gson().fromJson(json, Student.class); System....

October 11, 2022 · 2 min · 374 words · Andrew

How to Calculate Powers of Integers in Java

If you need to calculate the powers of Integers in Java, then you can do one of the following: Option 1 – Using for loops public class Power { public static void main(String args[]){ int number = 5; int power = 3; int result = calculatePower(number,power); System.out.println(number+"^"+power+"="+result); } static int calculatePower(int num, int power){ int answer = 1; if (num > 0 && power==0){ return answer; } else if(num == 0 && power>=1){ return 0; } else{ for(int i = 1; i<= power; i++) answer = answer*num; return answer; } } } Option 2 – Using Recursion public class Power { public static void main(String args[]){ int number = 3; int power = 3; int result = CalculatePower(number,power); System....

October 10, 2022 · 1 min · 184 words · Andrew

How to Get Today’s Date in Java

If you need to get today’s date in Java, then you can do one of the following: Option 1 – Using LocalDate import java.time.LocalDate; public class GetTodayDate { public static void main(String[] args) { LocalDate todaysDate = LocalDate.now(); System.out.println(todaysDate); } } Option 2 – Using Calendar and SimpleDateFormat import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class GetTodayDate { public static void main(String[] args) { SimpleDateFormat dtf = new SimpleDateFormat("yyyy/MM/dd"); Calendar calendar = Calendar....

October 9, 2022 · 1 min · 113 words · Andrew

How to Make a Java Jar File Executable

Let’s say you have a Java project as follows: package ms.ao.something; public class MyProject { public static void main(String...args) throws Exception { System.out.println("Hello world!"); } } Now you want to build this and make it a self contained executable Jar. If you do a mvn clean install or mvn clean package, and try and run it as follows: java -jar target/my-java-1.0-SNAPSHOT.jar You will get the following error: no main manifest attribute, in target/my-java-1....

August 15, 2022 · 1 min · 132 words · Andrew

How to Read a File Line by Line in Java

If you need to read a file line by line in Java, then you can use one of the following three (3) options. Option 1 You can use the FileReader and BufferedReader packages as follows: File file = new File("./your/file.txt"); try (FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr);) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } Option 2 You can also read the lines using Files and Paths as follows:...

July 19, 2022 · 1 min · 139 words · Andrew

How can I clear or empty a StringBuilder

You can use Java’s StringBuilder to create and manipulate Strings as follows: StringBuilder sb = new StringBuilder(); sb.append("Some").append(" ").append("String"); System.out.println(sb.toString()); // "Some String" The following three (3) options allow you to clear or empty a StringBuilder. Option 1 – using setLength sb.setLength(0); Option 2 – reinstantiating sb = new StringBuilder(); Option 3 – using delete sb.delete(0, sb.length());

July 18, 2022 · 1 min · 57 words · Andrew