Java Articles

Page 16 of 450

Convert one base to other bases in a single Java Program

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

Let’s say we have an Octal number. To convert Octal to other bases like binary, hexadecimal, etc, the Java code is as follows −Examplepublic class Demo{    public static String base_convert(String num, int source, int destination){       return Integer.toString(Integer.parseInt(num, source), destination);    }    public static void main(String[] args){       String my_num = "345";       int source = 8;       int destination = 2;       System.out.println("Converting the number from octal to binary: "+ base_convert (my_num, source, destination));       destination = 10;       System.out.println("Converting the number from ...

Read More

Java program to generate random numbers within a given range and store in a list

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 447 Views

To generate random numbers in a given range, the Java code is as follows −Exampleimport java.util.Random; import java.util.*; public class Demo{    public static void main(String args[]){       Random my_rand = new Random();       List my_list_1 = new ArrayList();       int v_1 = my_rand.nextInt(1000);       int v_2 = my_rand.nextInt(967);       int v_3 = my_rand.nextInt(1050);       int v_4 = my_rand.nextInt(10000);       int v_5 = my_rand.nextInt(100);       my_list_1.add(v_1);       my_list_1.add(v_2);       my_list_1.add(v_3);       my_list_1.add(v_4);       my_list_1.add(v_5);       System.out.println("The random values in the list are : ");       for(int i=0; i

Read More

Java program to split the Even and Odd elements into two different lists

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 903 Views

To split the Even and Odd elements into two different lists, the Java code is as follows −Exampleimport java.util.Scanner; public class Demo{    public static void main(String[] args){       int n, j = 0, k = 0;       Scanner s = new Scanner(System.in);       System.out.println("Enter the number of elements required :");       n = s.nextInt();       int my_arr[] = new int[n];       int odd_vals[] = new int[n];       int even_vals[] = new int[n];       System.out.println("Enter the elements of the array(even and add numbers) :"); ...

Read More

Program to Iterate over a Stream with Indices in Java 8

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 672 Views

To iterate over a Stream with Indices in Java 8, the code is as follows −Exampleimport java.util.stream.IntStream; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; public class Demo{    public static void main(String[] args){       String[] my_array = { "T", "h", "i", "s", "s", "a", "m", "p", "l", "e" };       AtomicInteger my_index = new AtomicInteger();       System.out.println("The elements in the string array are :");       Arrays.stream(my_array).map(str -> my_index.getAndIncrement() + " -> " + str).forEach(System.out::println);    } }OutputThe elements in the string array are : 0 -> T 1 -> h 2 -> i 3 -> ...

Read More

Java program to find Maximum and minimum element’s position in a list

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 449 Views

To find the maximum and minimum element’s position in a list, the Java program is as follows −Exampleimport java.util.*; import java.util.Arrays; import java.util.Collections; public class Demo{    public static int index_val(int my_arr[], int t){       if (my_arr == null){          return -1;       }       int len = my_arr.length;       int i = 0;       while (i < len){          if (my_arr[i] == t){             return i;          } else {           ...

Read More

Find the uncommon values concatenated from both the strings in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 316 Views

To find the uncommon values concatenated from both the strings in Java, the code is as follows −Exampleimport java.util.*; import java.lang.*; import java.io.*; public class Demo{    public static String concat_str(String str_1, String str_2){       String result = "";       int i;       HashMap my_map = new HashMap();       for (i = 0; i < str_2.length(); i++)       my_map.put(str_2.charAt(i), 1);       for (i = 0; i < str_1.length(); i++)       if (!my_map.containsKey(str_1.charAt(i)))       result += str_1.charAt(i);       else       my_map.put(str_1.charAt(i), ...

Read More

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 &lsquo;public&rsquo; with &lsquo;private&rsquo; in &ldquo;main&rdquo; 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
Showing 151–160 of 4,495 articles
« Prev 1 14 15 16 17 18 450 Next »
Advertisements