Found 33676 Articles for Programming

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

674 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

950 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

272 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

399 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

605 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

Java program to print unique values from a list

AmitDiwan
Updated on 08-Nov-2024 22:30:18

1K+ Views

In this article, we will learn to print unique values from a List in Java. This program will use a loop-based approach to identify and display only the values that appear once, skipping any duplicates. This approach is helpful when working with datasets where you want to remove repeated items and focus on distinct entries. Problem Statement Write a program in Java to print unique values from a list. Below is a demostration of the same − Input 55, 67, 99, 11, 54, 55, 88, 99, 1, 13, 45 Output The distinct elements in the array are 55 67 99 11 ... Read More

Java program to find all close matches of input string from a list

Alshifa Hasnain
Updated on 17-Dec-2024 03:34:20

743 Views

Finding close matches to a string from a list of words is a common problem in string manipulation and pattern recognition. This article demonstrates two effective approaches for solving this problem in Java. The first approach utilizes string encoding to identify similar patterns, while the second approach leverages the Levenshtein Distance algorithm to find approximate matches. String Encoding Approach The string encoding approach is a clever technique where each string is transformed into a unique encoded format. Strings with the same encoded pattern are considered matches HashMap A HashMap in Java is a collection that stores key-value pairs, where keys ... Read More

Advertisements