Found 7442 Articles for Java

Static and non static blank final variables in Java

AmitDiwan
Updated on 08-Jul-2020 10:28:41

1K+ Views

Static variable: Declared with the help of the keyword ‘static’, they are also known as class variables. They are defined within a constructor or outside a class function. When a variable is static, it is shared between all objects of the class, irrespective of the number of objects that are created.Demonstrating how the ‘static’ keyword, when used with variable works −Example Live Demopublic class Demo{    String name;    static String designation;    public void display_data(){       System.out.println("The name is: " + name);       System.out.println("The designation of this team members is : " + designation);    } ... Read More

Java program to find IP Address of the client

AmitDiwan
Updated on 08-Jul-2020 10:25:22

2K+ Views

To find the IP Address of the client, the Java code is as follows −Example Live Demoimport 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

Java program to check order of characters in string

Shriansh Kumar
Updated on 30-Sep-2024 16:03:43

2K+ Views

You are given a String, your task is to write a program in Java that checks the order of its characters. If the order is previously defined, you have to check if the characters are in the given order otherwise check for alphabetical order. Let's understand the problem statement with an example − Example Scenario: Input: str = "abcmnqxz"; Output: res = TRUE The given string is in alphabetical order. Using Iteration In this approach, use a for loop to iterate over the string and check if the value of character at the current place and the previous ... Read More

Java code to print common characters of two strings in alphabetical order

AmitDiwan
Updated on 15-Oct-2024 11:52:18

2K+ Views

In this article, we will learn to print common characters of two strings in alphabetical order using Java. The program uses arrays to count how often each letter appears in both strings and then compares these counts to identify the common characters. Problem Statement Write a Java program to print common characters of two strings in alphabetical order − Input my_str_1 = "itsasample" my_str_2 = "thisisasample" Output The common characters between the two strings in alphabetical order is :aaeilmpsst Steps to print common characters of two strings in alphabetical order Following are the steps to print common characters of two strings ... Read More

Find the second most repeated word in a sequence in Java

AmitDiwan
Updated on 08-Jul-2020 10:20:18

672 Views

To find the second most repeated word in a sequence in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo{    static String second_repeated(Vector my_seq){       HashMap my_map = new HashMap(my_seq.size()){          @Override          public Integer get(Object key){             return containsKey(key) ? super.get(key) : 0;          }       };       for (int i = 0; i < my_seq.size(); i++)       my_map.put(my_seq.get(i), my_map.get(my_seq.get(i))+1);       int first_val = Integer.MIN_VALUE;       int sec_val ... Read More

Find the first repeated word in a string in Java

AmitDiwan
Updated on 08-Jul-2020 10:17:39

949 Views

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

Find the uncommon values concatenated from both the strings in Java

AmitDiwan
Updated on 08-Jul-2020 10:16:20

268 Views

To find the uncommon values concatenated from both the strings in Java, the code is as follows −Example Live Demoimport 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       ... Read More

Java program to display Hostname and IP address

AmitDiwan
Updated on 28-Aug-2024 21:18:08

4K+ Views

In this article, we will learn to display the Hostname and IP address using Java. To display the Hostname and IP address we will be using the InetAddress class from the java.net package. We’ll write a simple program to fetch and print this information, and perform the exception handling to catch the exception if the data isn't found. Problem Statement Write a program in Java to display the Hostname and IP address. Below is a demonstration of the same − Output  The IP address is : 127.0.0.1The host name is : jdoodle Steps to display Hostname and IP address Following are ... Read More

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

AmitDiwan
Updated on 07-Jul-2020 09:54:25

397 Views

To find the maximum and minimum element’s position in a list, the Java program is as follows −Example Live Demoimport 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

Program to Iterate over a Stream with Indices in Java 8

AmitDiwan
Updated on 07-Jul-2020 09:52:03

601 Views

To iterate over a Stream with Indices in Java 8, the code is as follows −Example Live Demoimport 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

Advertisements