Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Java Articles
Page 14 of 450
How to check if a string is a valid keyword in Java?
To check if a string is a valid keyword in Java, the code is as follows −Exampleimport java.util.*; public class Demo{ static boolean valid_identifier(String my_str, int n){ if (!((my_str.charAt(0) >= 'a' && my_str.charAt(0) = 'A' && my_str.charAt(1) = 'a' && my_str.charAt(i) = 'A' && my_str.charAt(i) = '0' && my_str.charAt(i)
Read MoreComplex Numbers in Java
Complex numbers are those that have an imaginary part and a real part associated with it. They can be added and subtracted like regular numbers. The real parts and imaginary parts are respectively added or subtracted or even multiplied and divided.Examplepublic class Demo{ double my_real; double my_imag; public Demo(double my_real, double my_imag){ this.my_real = my_real; this.my_imag = my_imag; } public static void main(String[] args){ Demo n1 = new Demo(76.8, 24.0), n2 = new Demo(65.9, 11.23), temp; temp ...
Read MorePrecision Handling in Java
Let us see how precisions are handled in Java −Exampleimport java.io.*; import java.lang.*; public class Demo{ public static void main(String[] args){ double my_val = 34.909; System.out.println("The formatted value of 34.909 is "); System.out.println(String.format("%.7f", my_val)); double my_val_2 = 12.56; System.out.println("The formatted value of 12.56 is "); System.out.println(String.format("%.9f", my_val_2)); } }OutputThe formatted value of 34.909 is 34.9090000 The formatted value of 12.56 is 12.560000000A class named Demo contains the main function, where a double valued integer is declared, and it is formatted ...
Read MoreHow to clear screen using Java?
Following is the code to clear screen using Java −Examplepublic class Demo{ public static void main(String[] args){ System.out.print("\033[H\033[2J"); System.out.flush(); } }OutputThe screen would be clearedA class named Demo contains the main function. Here, the ANSI escape code is written, that clears the screen. The flush function resets the cursor to the top of the window screen.
Read MoreJava Program for Counting Sort
The Counting Sort counts the number of objects having distinct key values. Let us see an example −Note − The below code can be used with negative numbers as well.Exampleimport java.util.*; public class Demo{ static void count_sort(int[] arr){ int max_val = Arrays.stream(arr).max().getAsInt(); int min_val = Arrays.stream(arr).min().getAsInt(); int range = max_val - min_val + 1; int count[] = new int[range]; int result[] = new int[arr.length]; for (int i = 0; i < arr.length; i++){ count[arr[i] - min_val]++; ...
Read MoreJava Program for Comb Sort
The Comb Sort in Java eliminates the smaller values situated to the end of the list and the inversions are removed oby by one. Let us see an example −Exampleimport java.util.Arrays; public class Demo{ void comb_sort(int nums[]){ int len_gap = nums.length; float shrink_val = 1.3f; boolean swap = false; while (len_gap > 1 || swap) { if (len_gap > 1) { len_gap = (int)(len_gap / shrink_val); } swap = ...
Read MoreJava Program for Anagram Substring Search
Following is an example for Anagram Substring Search in Java −Examplepublic class Demo{ static final int max_val = 256; static boolean compare_vals(char my_arr_1[], char my_arr_2[]){ for (int i = 0; i < max_val; i++) if (my_arr_1[i] != my_arr_2[i]) return false; return true; } static void search_subs(String my_pattern, String my_text){ int pat_len = my_pattern.length(); int txt_len = my_text.length(); char[] count_pat = new char[max_val]; char[] count_txt = new char[max_val]; for (int ...
Read MoreJava Program for Bitonic Sort
In Bitonic Sort, the comparision is in predefined sequence (Bitonic sequence), not dependent on the data to be sorted. Let us see an example for Bitonic Sort Java program −Examplepublic class Demo{ void compare_swap(int my_arr[], int i, int j, int direction){ if ((my_arr[i] > my_arr[j] && direction == 1) || (my_arr[i] < my_arr[j] && direction == 0)){ int temp = my_arr[i]; my_arr[i] = my_arr[j]; my_arr[j] = temp; } } void merge_vals(int my_arr[], int low, int cnt, int direction){ ...
Read MoreString Formatting in Java using %
Followimg is the code to implement String formatting in Java using % −Examplepublic class Demo { public static void main(String args[]){ String my_str = " sample."; String concat_Str = String.format("This is a" + "%s", my_str); String format_str_1 = String.format("The value is %.4f", 78.92367); System.out.println(concat_Str); System.out.println(format_str_1); } }OutputThis is a sample. The value is 78.9237A class named Demo contains the main function. Here a string value is defined, which is used to format the string, by concatenating it to another variable. Similarly, a floating ...
Read MoreJava Program to find largest prime factor of a number
Following is the Java code to find the largest prime factor of a number −Exampleimport java.io.*; import java.util.*; public class Demo{ static long maxPrimeFactors( long val){ long max_prime = -1; while (val % 2 == 0) { max_prime = 2; val >>= 1; } for (int i = 3; i 2) max_prime = val; return max_prime; } public static void main(String[] args){ int val = 148592; ...
Read More