Java Articles

Page 18 of 450

Find the first repeated word in a string in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

To find the first repeated word in a string in Java, the code is as follows −Exampleimport java.util.*; public class Demo{    static char repeat_first(char my_str[]){       HashSet my_hash = new HashSet();       for (int i=0; i

Read More

Java program to find IP Address of the client

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 2K+ Views

To find the IP Address of the client, the Java code is as follows −Exampleimport java.net.*; import java.io.*; import java.util.*; import java.net.InetAddress; public class Demo{    public static void main(String args[]) throws Exception{       InetAddress my_localhost = InetAddress.getLocalHost();       System.out.println("The IP Address of client is : " + (my_localhost.getHostAddress()).trim());       String my_system_address = "";       try{          URL my_url = new URL("http://bot.whatismyipaddress.com");          BufferedReader my_br = new BufferedReader(new          InputStreamReader(my_url.openStream()));          my_system_address = my_br.readLine().trim();       }     ...

Read More

Replace null values with default value in Java Map

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 1K+ Views

To replace null values with default value in Java Map, the code is as follows −Exampleimport java.util.*; import java.util.stream.*; public class Demo{    public static Map null_vals(Map my_map, T def_val){       my_map = my_map.entrySet().stream().map(entry -> {          if (entry.getValue() == null)          entry.setValue(def_val);          return entry;       })       .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));       return my_map;    }    public static void main(String[] args){       Map my_map = new HashMap();       my_map.put(1, null);       my_map.put(2, 56);     ...

Read More

Replacing ‘public’ with ‘private’ in “main” in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 455 Views

When ‘public’ is used in ‘main’ −Examplepublic class Demo{    public static void main(String args[]){       System.out.println("This is a sample only");    } }OutputThis is a sample onlyA class named Demo contains the main function that is public. It has a print function, which successfully compiles, executes and prints the message on the console.When ‘public’ is replaced with ‘private’Examplepublic class Demo{    private static void main(String args[]){       System.out.println("This is a sample only");    } }OutputError: Main method not found in class Demo, please define the main method as: public static void main(String[] args) or a ...

Read More

Java Program to check if count of divisors is even or odd

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 251 Views

To check if the count of divisors is even or odd, the Java code is as follows −Exampleimport java.io.*; import java.math.*; public class Demo{    static void divisor_count(int n_val){       int root_val = (int)(Math.sqrt(n_val));       if (root_val * root_val == n_val){          System.out.println("The number of divisors is an odd number");       }else{          System.out.println("The number of divisors is an even number");       }    }    public static void main(String args[]) throws IOException{       divisor_count(25);    } }OutputThe number of divisors is an odd ...

Read More

Java Program to check whether it is possible to make a divisible by 3 number using all digits in an array

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 276 Views

To check whether it is possible to make a divisible by 3 number using all digits in an array, the Java code is as follows −Exampleimport java.io.*; import java.util.*; public class Demo{    public static boolean division_possible(int my_arr[], int n_val){       int rem = 0;       for (int i = 0; i < n_val; i++)       rem = (rem + my_arr[i]) % 3;       return (rem == 0);    }    public static void main(String[] args){       int my_arr[] = { 66, 90, 87, 33, 123};   ...

Read More

Java Program to Convert Iterator to Spliterator

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 173 Views

To convert Iterator to Spliterator, the Java code is as follows −Exampleimport java.util.*; public class Demo{    public static Spliterator getspiliter(Iterator iterator){       return Spliterators.spliteratorUnknownSize(iterator, 0);    }    public static void main(String[] args){       Iterator my_iter = Arrays.asList(56, 78, 99, 32, 100, 234).iterator();       Spliterator my_spliter = getspiliter(my_iter);       System.out.println("The values in the spliterator are : ");       my_spliter.forEachRemaining(System.out::println);    } }OutputThe values in the spliterator are : 56 78 99 32 100 234A class named Demo contains a function named ‘getspiliter’ that returns a spliterator. In the ...

Read More

Java program to count the characters in each word in a given sentence

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 583 Views

To count the characters in each word in a given sentence, the Java code is as follows −Exampleimport java.util.*; public class Demo{    static final int max_chars = 256;    static void char_occurence(String my_str){       int count[] = new int[max_chars];       int str_len = my_str.length();       for (int i = 0; i < str_len; i++)       count[my_str.charAt(i)]++;       char ch[] = new char[my_str.length()];       for (int i = 0; i < str_len; i++){          ch[i] = my_str.charAt(i);          int find = 0;          for (int j = 0; j

Read More

Java Program to Count trailing zeroes in factorial of a number

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 880 Views

To count trailing zeroes in factorial of a number, the Java code is as follows −Exampleimport java.io.*; public class Demo{    static int trailing_zero(int num){       int count = 0;       for (int i = 5; num / i >= 1; i *= 5){          count += num / i;       }       return count;    }    public static void main (String[] args){       int num = 1000000;       System.out.println("The number of trailing zeroes in " + num +" factorial is " +   ...

Read More

Java program to expand a String if range is given?

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 407 Views

To expand a String if range is given, the Java code is as follows −Examplepublic class Demo {    public static void expand_range(String word) {       StringBuilder my_sb = new StringBuilder();       String[] str_arr = word.split(", ");       for (int i = 0; i < str_arr.length; i++){          String[] split_str = str_arr[i].split("-");          if (split_str.length == 2){             int low = Integer.parseInt(split_str[0]);             int high = Integer.parseInt(split_str[split_str.length - 1]);             while (low

Read More
Showing 171–180 of 4,495 articles
« Prev 1 16 17 18 19 20 450 Next »
Advertisements