0
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.

madth3
  • 7,275
  • 12
  • 50
  • 74
Mr.Yoso
  • 3
  • 1
  • 1
  • 2

5 Answers5

3
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()); 
    }
1218985
  • 7,531
  • 2
  • 25
  • 31
1

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.

CosminO
  • 5,018
  • 6
  • 28
  • 50
0

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

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0
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);   
}
user93353
  • 13,733
  • 8
  • 60
  • 122
0
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);   
   }

}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Pranav
  • 1,487
  • 2
  • 25
  • 53
  • While this code snippet may solve the question, [including an explanation](//s.tk/meta/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Filnor Dec 13 '18 at 15:48