如何从字符串中分离字母和符号。(几乎完成)

时间:2013-02-27 16:05:34

标签: java string javac symbols letters

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);   
   }

}

好的,我已经有了这个代码,用于分隔我输入的数字和字母。问题是,当我输入符号例如(^,+, - ,%,*)时,它也表示为一封信。

我想要做的是将符号与字母分开,就像我在数字和字母上所做的那样,并为它做另一个输出。

5 个答案:

答案 0 :(得分: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()); 
    }

答案 1 :(得分:1)

您正在测试角色isDigitelse是否将其视为字母。 好吧,如果它不是数字,所有其他情况都会转到代码中的else。为这些符号创建else案例。

答案 2 :(得分:0)

由于家庭作业很充实,只需看看documentation of Character,它具有很好的功能isDigit

答案 3 :(得分: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);   
}

答案 4 :(得分: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);   
   }

}