mbish75 mbish75 - 2 months ago 6
Java Question

counting special characters in a user generated string

So I have this program that is supposed to to take any user generated string and display the amounts of white space, letters, numbers, and special characters. I do not want to duplicate a question. It seems to be a little more specific then talked about in other posts.

My error lies in the special characters. The specific error is return 0. I referenced stack overflow already in about every discussion they had on the matter. Which helped me form the special character method.

I have to keep the main method clear and call aforementioned methods

public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
String userInput;
System.out.println("Please enter a string");
userInput = kbd.nextLine();

countletter(userInput);
countnumber(userInput);
countspecial(userInput);
countSpace(userInput);

}

public static void countletter(String userInput) {
int countletter = 0;
for (int i = 0; i < (userInput.length() - 1); i++) {
char location = userInput.charAt(i);
boolean x = Character.isLetter(location);
if (x) {
countletter++;
}

}
System.out.println("The number of Letters is: " + countletter);
}

public static void countnumber(String userInput) {
int countnumber = 0;

for (int i = 0; i < userInput.length() - 1; i++) {
char location = userInput.charAt(i);
boolean x = Character.isDigit(location);
if (x) {
countnumber++;
}

}
System.out.println("The number of digits is: " + countnumber);
}

public static void countSpace(String userInput) {
int countSpace = 0;

for (int i = 0; i < userInput.length() - 1; i++) {
char location = userInput.charAt(i);
boolean x = Character.isWhitespace(location);
if (x) {
countSpace++;
}

}
System.out.println("The number of white spaces is: " + countSpace);
}

public static void countspecial(String userInput) {

if (userInput == null || userInput.trim().isEmpty()) {
return 0;
}
int countSpecial = 0;
for (int i = 0; i < userInput.length(); i++) {
if (userInput.substring(i, 1).matches("[^A-Za-z0-9 ]")) {
countSpecial++;
}
}
System.out.println("The number of special chars is: " + countSpecial++);
}
}


My initial attempt at countSpecial:

public static void countspecial(String userInput) {
int countSpecial = 0;

for (int i = 0; i < (userInput.length() - 1); i++) {
if (userInput.substring(i, 1).matches("[^A-Za-z0-9]")) {
countSpecial++;
}

}
System.out.println("The number of special chars is: " + countSpecial++);
}
}


Where am I going wrong and why?

Answer

in your initial attemp you have problem with your substring substring second parameter would be the index of your last character in the substring +1, and so you need to iterate until userInput.length() not userInput.length() -1. And i dont know why would you increment the count after printing it , it actually does nothing but still makes no sense.

      public static void countspecial(String userInput) {
        if (userInput == null || userInput.trim().isEmpty()) {
          return ;
        }
        int countSpecial = 0;

        for (int i = 0; i < userInput.length(); i++) {
           if (userInput.substring(i, i+1).matches("[^A-Za-z0-9]")) {
              countSpecial++;
           }

        }
        System.out.println("The number of special chars is: " + countSpecial);
        }

A better way instead of subtring you could use "(userInput.charAt(i)+"").matches".

But a better solution would be counting all the digits and letters and subtract their sum form the total length of the string , this would give you the number of special characters as it is not recommended to use regex for simple requirement like this.