检查字符是否是Java中的特殊字符

use*_*604 8 java special-characters

可能重复:
JAVA:检查字符串是否有特殊字符

我是一名新手程序员,正在寻找帮助,确定角色是否是一个特殊角色.我的程序要求用户输入文件名,程序读取文件中的文本并确定文本中有多少空格,数字,字母和特殊字符.我已完成代码以确定空格,数字和字母,但不确定如何检查字符是否是特殊字符.我们对您提供的任何帮助表示赞赏,如果有些问题不够明确,我可以尝试详细说明.到目前为止我的代码是:

import java.util.Scanner;
import java.io.*;

public class TextFile{

public static void main(String[] args){

  Scanner input = new Scanner (System.in);
  String fileName;
  boolean goodName = false;
  int blankCount = 0;
  int letterCount = 0;
  int digitCount = 0;
  int specialcharCount = 0;
  String currentLine;
  char c;
  Scanner lineFile= null;
  FileReader infile;

  System.out.println("Please enter the name of the file: ");
  fileName = input.nextLine();

  while (!goodName) {
    try{
      infile = new FileReader(fileName);
      lineFile = new Scanner(infile);

      goodName= true;
    }
    catch(IOException e) {
      System.out.println("Invalid file name, please enter correct file name: ");
      fileName=input.nextLine();
    }
  }

  while (lineFile.hasNextLine()){
    currentLine = lineFile.nextLine();
    for(int j=0; j<currentLine.length();j++){
      c=currentLine.charAt(j);
      if(c== ' ') blankCount++;
      if(Character.isDigit(c)) digitCount++;
      if(Character.isLetter(c)) letterCount++;
      if() specialcharCount++;
    }
  }
}
}
Run Code Online (Sandbox Code Playgroud)

我需要在最后添加if语句来增加specialcharCount.

jah*_*roy 12

此方法检查String是否包含特殊字符(基于您的定义).

/**
 *  Returns true if s contains any character other than 
 *  letters, numbers, or spaces.  Returns false otherwise.
 */

public boolean containsSpecialCharacter(String s) {
    return (s == null) ? false : s.matches("[^A-Za-z0-9 ]");
}
Run Code Online (Sandbox Code Playgroud)

您可以使用相同的逻辑来计算字符串中的特殊字符,如下所示:

/**
 *  Counts the number of special characters in s.
 */

 public int getSpecialCharacterCount(String s) {
     if (s == null || s.trim().isEmpty()) {
         return 0;
     }
     int theCount = 0;
     for (int i = 0; i < s.length(); i++) {
         if (s.substring(i, 1).matches("[^A-Za-z0-9 ]")) {
             theCount++;
         }
     }
     return theCount;
 }
Run Code Online (Sandbox Code Playgroud)

另一种方法是将所有特殊字符放在String中并使用String.contains:

/**
 *  Counts the number of special characters in s.
 */

 public int getSpecialCharacterCount(String s) {
     if (s == null || s.trim().isEmpty()) {
         return 0;
     }
     int theCount = 0;
     String specialChars = "/*!@#$%^&*()\"{}_[]|\\?/<>,.";
     for (int i = 0; i < s.length(); i++) {
         if (specialChars.contains(s.substring(i, 1))) {
             theCount++;
         }
     }
     return theCount;
 }
Run Code Online (Sandbox Code Playgroud)

注意:您必须逃离反斜线"字符用反斜杠.


以上是如何一般地解决该问题的示例.

对于问题中所述的确切问题,@ LanguagesNamedAfterCoffee的答案是最有效的方法.


小智 6

看一下类java.lang.Character静态成员方法(isDigit、isLetter、isLowerCase,...)

例子:

String str = "Hello World 123 !!";
int specials = 0, digits = 0, letters = 0, spaces = 0;
for (int i = 0; i < str.length(); ++i) {
   char ch = str.charAt(i);
   if (!Character.isDigit(ch) && !Character.isLetter(ch) && !Character.isSpace(ch)) {
      ++specials;
   } else if (Character.isDigit(ch)) {
      ++digits;
   } else if (Character.isSpace(ch)) {
      ++spaces;
   } else {
      ++letters;
   }
}
Run Code Online (Sandbox Code Playgroud)