Posts

Showing posts with the label Java

Recyclerview steps

in the main activity, add recycler view activity_main.xml <? xml version ="1.0" encoding ="utf-8" ?> <androidx.constraintlayout.widget.ConstraintLayout xmlns: android ="http://schemas.android.com/apk/res/android" xmlns: app ="http://schemas.android.com/apk/res-auto" xmlns: tools ="http://schemas.android.com/tools" android :layout_width ="match_parent" android :layout_height ="match_parent" tools :context =".MainActivity" > <androidx.recyclerview.widget.RecyclerView android :layout_width ="match_parent" android :layout_height ="match_parent" android :id ="@+id/recyclerView" /> </androidx.constraintlayout.widget.ConstraintLayout> in the layout file, add a layout to add the items to display item_list, make the layout as MaterialCardView. Add linear layout and add the required stuffs with match parent, gravity...

Program to print the duplicate characters in a string

public static ArrayList<String> checkForDuplicates(String enteredString) { char[] data = enteredString.toCharArray(); ArrayList<String> duplicateChars = null; for(int i = 0; i < enteredString.length(); i++) { for(int j = i+1; j < enteredString.length(); j++) { if(data[i] == (data[j])) { System.out.println("Duplicate Characters "+data[j]); break; } } } return duplicateChars; } public static void main(String[] args) { checkForDuplicates("madam"); }

Program to check if entered string is Palindrome in Java

 public static boolean checkForPalindrome(String reverseString) { StringBuffer reversedString = new StringBuffer(); char[] reverseArray = reverseString.toCharArray(); for(int i = reverseArray.length-1; i>=0; i--) { reversedString.append(reverseArray[i]); } if(reverseString.equals(reversedString.toString())) { return true; } else { return false; } } public static void main(String[] args) { boolean reversedString = checkForPalindrome("MADAM"); if(reversedString) { System.out.println("Entered string is a Palindrome"); } else { System.out.println("Entered string is not a Palindrome"); } }

Program to Reverse a string in Java

public static String reverseString(String reverseString) { StringBuffer reversedString = new StringBuffer(); char[] reverseArray = reverseString.toCharArray(); for(int i = reverseArray.length-1; i>=0; i--) { reversedString.append(reverseArray[i]); } return reversedString.toString(); } public static void main(String[] args) { String reversedString = reverseString("Keshav"); System.out.println("Reversed String "+reversedString); }