Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a character is a special character in Java [duplicate]

Tags:

Possible Duplicate:
JAVA: check a string if there is a special character in it

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

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


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!