Write a java program to Flush Characters.

Write a program to read a string from the user and remove all the alphabets and spaces from the String, and only store special characters and digit in the output String. Print the output string.

The return type (String) should return the character removed string.

Input and Output Format

  • Input consists of a string.
  • The output consists of a String (character removed string).

Refer sample output for formatting specifications

Sample Input :

codebun$#45Ant

Sample Output :

$#45

Program to flush Characters in Java

Following are the steps to flush characters in Java:

  • Input string from the user.
  • Pass the string to getvalues() method.
  • Inside the method, create an empty string buffer to hold the updated string.
  • Use for loop to traverse over the string and get each character for every iteration and store it in variable ‘a’.
  • Now, check if it is not alphabet then, append the character to string buffer. At last, return it to the user by converting it into a string.
import java.util.Scanner;

public class Main {
  /**
   * @param args
   */
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.nextLine();

    String s2 = getvalues(s1);
    System.out.println(s2);
  }

  public static String getvalues(String s1) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < s1.length(); i++) {
      char a = s1.charAt(i);
      if (!Character.isAlphabetic(a))
        sb.append(a);
    }
    return sb.toString();
  }
}

Output

Java Program to Flush Digits

Write a program to read a string from the user and remove all the digits and spaces from the String and only alphabets in the output String. Print the output string.

The return type (String) should return the integer removed string.

Input and Output Format

  • Input consists of a string.
  • The output consists of a String (digits removed string).

Refer sample output for formatting specifications

Sample Input :

codebun$#45Ant

Sample Output :

codebun

Following are the steps to flush digits in Java:

  • Input string from the user.
  • Pass the string to getvalues() method.
  • Inside the method, create an empty string buffer to hold the updated string.
  • Use for loop to traverse over the string and get each character for every iteration and store it in variable ‘a’.
  • Now, check if it is an alphabet then, append the character to string buffer. At last, return it to the user by converting it into a string.
package com.demo2;

import java.util.Scanner;

public class FlushCharacters {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        String s2 = getvalues(s1);
        System.out.println(s2);
    }

    public static String getvalues(String s1) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s1.length(); i++) {
            char a = s1.charAt(i);
            if (Character.isAlphabetic(a)) sb.append(a);
        }
        return sb.toString();
    }
}

Output

Program to flush UpperCase Characters in Java

Write a program to read a string from the user and remove all the lowercase from the String and only lower case alphabets in the output String. Print the output string.

The return type (String) should return the lowercase string.

Input and Output Format

  • Input consists of a string.
  • The output consists of a String (uppercase removed string).

Refer sample output for formatting specifications

Sample Input :

CodeBun

Sample Output :

odeun

Following are the steps to flush uppercase in Java:

  • Input string from the user.
  • Pass the string to getvalues() method.
  • Inside the method, create an empty string buffer to hold the updated string.
  • Use for loop to traverse over the string and get each character for every iteration and store it in variable ‘a’.
  • Now, check if it is a lowercase character then, append the character to the string buffer. At last, return it to the user by converting it into a string.
package com.demo2;

import java.util.Scanner;

public class FlushCharacters {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        String s2 = getvalues(s1);
        System.out.println(s2);
    }

    public static String getvalues(String s1) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s1.length(); i++) {
            char a = s1.charAt(i);
            if (Character.isLowerCase(a)) sb.append(a);
        }
        return sb.toString();
    }
}

Output

Thus, in this way, we learn how to flush digits and characters in Java.