Java Articles

Page 17 of 450

Java Program to find reminder of array multiplication divided by n

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 264 Views

To find reminder of array multiplication divided by n, the Java code is as follows −Exampleimport java.util.*; import java.lang.*; public class Demo{    public static int remainder(int my_arr[], int arr_len, int val){       int mul_val = 1;       for (int i = 0; i < arr_len; i++)       mul_val = (mul_val * (my_arr[i] % val)) % val;       return mul_val % val;    }    public static void main(String argc[]){       int[] my_arr = new int []{ 35, 100, 69, 99, 27, 88, 12, 25 };   ...

Read More

Java Program to Find the closest pair from two sorted arrays

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 381 Views

To find the closest pair from two sorted array, the Java code is as follows −Examplepublic class Demo {    void closest_pair(int my_arr_1[], int my_arr_2[], int arr_1_len, int arr_2_len, int sum){       int diff = Integer.MAX_VALUE;       int result_l = 0, result_r = 0;       int l = 0, r = arr_2_len-1;       while (l=0){          if (Math.abs(my_arr_1[l] + my_arr_2[r] - sum) < diff){             result_l = l;             result_r = r;             diff ...

Read More

Unreachable Code Error in Java

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

Unreachable code error occurs when the code can’t be compiled due to a variety of reasons, some of which include: infinite loop, return statement before the unreachable line of code.Let us see an example −Examplepublic class Demo{    public static void main(String args[]){       int val = 5;       for (;;){          if (val == 5){             break;             System.out.println("If the condition is not true, this line would be printed. ");          }       }    } }Output/Demo.java:11: ...

Read More

Using TreeMap to sort User-defined Objects in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 238 Views

To sort user-defined object in Java, the code is as follows −Exampleimport java.io.*; import java.util.*; public class Demo{    static void sort_objects(String my_data){       String[] my_vals = my_data.split(" ");       Map my_map = new TreeMap();       for (int i = 1; i < my_vals.length; i += 2){          int my_age = Integer.parseInt(my_vals[i]);          String name = my_vals[i - 1];          if (my_map.containsKey(my_age)){             ArrayList my_list = my_map.get(my_age);             my_list.add(name);             ...

Read More

Using predefined class name as Class or Variable name in Java

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

Using a predefined class name as a class nameLet us see an example −Examplepublic class Number{    public static void main (String[] args){       System.out.println("Pre-defined class name can be used as a class name");    } }OutputPre-defined class name can be used as a class nameThe class Number has a main function that displays a message when it is executed. The main function takes string values as arguments.Using a predefined class name as a variable nameLet us see an example −Examplepublic class String{    public static void main (java.lang.String[] args){       System.out.println("Pre-defined class name can be ...

Read More

Using underscore in Numeric Literals in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 231 Views

Followig is the code showing how to use underscore in numeric literals in Java −Examplepublic class Demo{    public static void main (String[] args) throws java.lang.Exception{       int my_num_1 = 6_78_00_120;       System.out.println("The number is : " + my_num_1);       long my_num_2 = 2_00_11_001;       System.out.println("The number is : " + my_num_2);       float my_num_3 = 4.01_981F;       System.out.println("The number is : " + my_num_3);       double my_num_4 = 12.89_46_061;       System.out.println("The number is : " + my_num_4);    } }OutputThe number is : ...

Read More

Ways to read input from console in Java

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

Let us see some ways to read input from console in Java −Exampleimport java.util.Scanner; public class Demo{    public static void main(String args[]){       Scanner my_scan = new Scanner(System.in);       String my_str = my_scan.nextLine();       System.out.println("The string is "+my_str);       int my_val = my_scan.nextInt();       System.out.println("The integer is "+my_val);       float my_float = my_scan.nextFloat();       System.out.println("The float value is "+my_float);    } }OutputThe string is Joe The integer is 56 The float value is 78.99A class named Demo contains the main function. An instance of the ...

Read More

Structure and Members of the Java Program

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 440 Views

While writing any piece of code in Java, there is a certain set of rules and regulations that need to be followed, that is considered as a standard. For example − A class contains variables, and functions. The functions can be used to work with the variables. Classes can be extended, and improvised too.Basic structureList of packages that are imported; public class {    Constructor (can be user defined or implicitly created)    {       Operations that the constructor should perform;    }    Data elements/class data members;    User-defined functions/methods;    public static void main (String ...

Read More

Sum of list with stream filter in Java

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

To get sum of list with stream filter in Java, the code is as follows −Exampleimport java.util.*; public class Demo {    public static void main(String[] args)    {       List my_list = new ArrayList();       my_list.add(11);       my_list.add(35);       my_list.add(56);       my_list.add(78);       my_list.add(91);       System.out.println(sum(my_list));    }    public static int sum(List my_list)    {       System.out.println("In the main function, the sum of list with filter is ");       return my_list.stream().filter(i -> i > 5).mapToInt(i -> i).sum();    } }OutputIn ...

Read More

How to input multiple values from user in one line in Java?

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

To input multiple values from user in one line, the code is as follows −Exampleimport java.util.Scanner; public class Demo {    public static void main(String[] args) {       System.out.print("Enter two floating point values : ");       Scanner my_scan = new Scanner(System.in);       double double_val = my_scan.nextFloat();       int int_val = my_scan.nextInt();       System.out.println("The floating point value is : " + double_val + " and the integer value is : "       + int_val);    } }Input56.789 99OutputEnter two floating point values : The floating point value is : ...

Read More
Showing 161–170 of 4,496 articles
« Prev 1 15 16 17 18 19 450 Next »
Advertisements