检查字符在Java中是否为特殊字符 [英] Checking if a character is a special character in Java

查看:61
本文介绍了检查字符在Java中是否为特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
JAVA:检查字符串是否存在

我是一名新手程序员,正在寻求帮助确定某个字符是否为特殊字符.我的程序要求用户输入文件名,然后程序读取文件中的文本并确定文本中有多少空格,数字,字母和特殊字符.我已经完成确定空格,数字和字母的代码,但是不确定如何检查字符是否为特殊字符.您能提供的任何帮助将不胜感激,如果有一些不清楚的地方,我可以尝试详细说明.到目前为止,我的代码是:

I am a novice programmer and am looking for help determining if a character is a special character. My program asks the user to input the name of a file, and the program reads the text in the file and determines how many blanks spaces, digits, letters, and special characters are in the text. I have the code completed to determine the blanks, digits, and letters, however am unsure of how to check if a character is a special character. Any help you can offer is appreciated and if something was not clear enough I can try to elaborate. My code so far is:

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

我需要在if语句的末尾添加一些内容以增加specialcharCount.

I need something to put in the if statement at the end to increment specialcharCount.

推荐答案

此方法检查字符串是否包含特殊字符(根据您的定义).

This method checks if a String contains a special character (based on your definition).

/**
 *  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 ]");
}

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

You can use the same logic to count special characters in a string like this:

/**
 *  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;
 }

另一种方法是将所有特殊字符放入String中,并使用

Another approach is to put all the special chars in a String and use 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;
 }

注意 :您必须使用反斜杠转义反斜杠"字符>

以上是通常如何解决此问题的示例.

The above are examples of how to approach this problem in general.

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

For your exact problem as stated in the question, the answer by @LanguagesNamedAfterCoffee is the most efficient approach.

这篇关于检查字符在Java中是否为特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆