Question

import java.util.Scanner;

public class Separate {

   public static void main(String[] args) {
         Scanner user_input = new Scanner( System.in ); 
    String variable;
    System.out.print("Enter Variable:");
      variable = user_input.next();
          Separate(variable);
   }

   public static void Separate(String str) {
          String number = "";
          String letter = "";
      String symbol = "";
          for (int i = 0; i < str.length(); i++) {
                 char a = str.charAt(i);
                 if (Character.isDigit(a)) {
                       number = number + a;

                 } else {
                       letter = letter + a;


         }
          }
          System.out.println("Alphabets in string:"+letter);
          System.out.println("Numbers in String:"+number);   
   }

}

Okay, i already have this code which separate the Numbers and Letters that i Input. The problem is, when ever i input Symbols for example (^,+,-,%,*) it also states as a Letter.

What i want to do is to separate the symbol from letters just like what i did on Numbers and Letters and make another output for it.

Was it helpful?

Solution 2

You are testing if the character isDigit, else treat it as a letter. Well, if it is not a digit, all other cases go to else, in your code. Create an else case for those symbols also.

OTHER TIPS

public static void separate(String string) {
        StringBuilder alphabetsBuilder = new StringBuilder();
        StringBuilder numbersBuilder = new StringBuilder();
        StringBuilder symbolsBuilder = new StringBuilder();
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
            if (Character.isAlphabetic(ch)) {
                alphabetsBuilder.append(ch);
            } else if (Character.isDigit(ch)) {
                numbersBuilder.append(ch);
            } else {
                symbolsBuilder.append(ch);
            }
        }
        System.out.println("Alphabets in string: " + alphabetsBuilder.toString());
        System.out.println("Numbers in String: " + numbersBuilder.toString());
        System.out.println("Sysmbols in String: " + symbolsBuilder.toString()); 
    }

As this reeks of homework, just see the documentation of Character, which had that nice function isDigit.

public static void Separate(String str) 
{
    String number = "";
    String letter = "";
    String symbol = "";

    for (int i = 0; i < str.length(); i++) 
    {
        char a = str.charAt(i);
        if (Character.isDigit(a)) 
        {
            number = number + a;
            continue;
        } 
        if(Character.isLetter(a))
        {
            letter = letter + a;
        }
        else
        {
            symbol = symbol + a;
        }
    }
    System.out.println("Alphabets in string:"+letter);
    System.out.println("Numbers in String:"+number);   
}
import java.util.Scanner;

public class Separate {

public static void main(String[] args) {

      Scanner user_input = new Scanner( System.in );

      String variable;

      System.out.print("Enter Variable:");

      variable = user_input.next();

      Separate(variable);
   }

         public static void Separate(String str) 
         {
          String number = "";

          String letter = "";

          String symbol = "";

          for (int i = 0; i < str.length(); i++) {

                 char a = str.charAt(i);

                 if (Character.isDigit(a)) {
                       number = number + a;

                 } else if (Character.isLetter(a)) {
                       letter = letter + a;
                 } else {
                       symbol = symbol + a;
         }
       }
     System.out.println("Alphabets in string:"+letter);
     System.out.println("Numbers in String:"+number);
     System.out.println("Symbols in String:"+symbol);   
   }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top