2016-10-03 95 views
0

所以我有这个程序应该采取任何用户生成的字符串并显示空白,字母,数字和特殊字符的数量。我不想重复一个问题。这似乎是更具体一些,然后在其他职位谈到。计算用户生成的字符串中的特殊字符

我的错误在于特殊字符。具体的错误是返回0.我引用堆栈溢出已经在他们对此事的每一个讨论。这帮助我形成了特殊的角色方法。

我必须保持的主要方法明确和调用上述方法

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

我在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++); 
} 
} 

我要去哪里错了,为什么?

回答

0
在初始学尝试

你有问题,你的子 子第二个参数是子串+1你的最后一个字符的索引,所以你需要迭代,直到userInput.length()不userInput.length() -1。 我不知道为什么打印后你会增加计数,它实际上什么也没做,但仍然没有意义。

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

一个更好的办法,而不是subtring您可以使用 “(userInput.charAt(我)+” “)。比赛”。

但更好的解决方案是计算所有数字和字母,并从字符串的总长度中减去它们的总和,这会给你特殊字符的数量,因为它不被推荐使用正则表达式来满足这样简单的要求。

+0

最后的增量是一个意外,我道歉。你的解释有助于我的理解。谢谢。 – mbish75

+0

if(userInput == null || userInput.trim()。isEmpty()){ return; 将userInput设置为null之后trim()。isEmpty())实际上是在做什么,我试图理解这行代码。我还没有了解.trim – mbish75

+0

trim()将从字符串中删除前导和尾随空白 –

0

查看子字符串的javadoc。

一些使用子字符串风格的语言(int beginIndex,int length)其中beginIndex是开始子字符串的位置,长度是子字符串中有多少个字符。

Java使用子字符串(int beginIndex,int endIndex)其中beginIndex是您开始子字符串的位置,endIndex是结尾字符的位置的

userInput.substring(i, 1); 

其中i = 2意味着您想要在索引位置2开始子字符串,并在索引位置1结束并抛出异常。

-1

更简单,不要循环。只需更换所有非特殊字符并计算长度:

int countSpecial = userInput.replaceAll("[A-Za-z0-9\\s]", "").length(); 

您可以将此技术应用于所有角色类别。

+0

子串(i,1)将永远不会工作 –

+0

请尝试阅读javadocs :) –

+0

@AmerQarabsa有什么子串要做与我的答案(顺便说一句)? – Bohemian

0

如果您将字母,数字或空白字符以外的其他字符定义为“特殊”,那么您应该以与其他测试相同的方式进行测试,因此您不会错过任何内容,因为定义不同什么构成一个字母或数字。对于任何Unicode字母,Character.isLetter()将返回true,例如,不仅仅是ASCII A-Z或a-z。为了保持一致性,你应该写:

char c = userInput.charAt(i); 
if (!Character.isDigit(c) && !Character.isLetter(c) && !Character.isWhiteSpace(c)) { 
    countSpecial++; 
} 

但在你原来的代码的问题是,要String.substring()的第二个参数是“到”指数,而不是长度。所以,如果你打算用正则表达式来做到这一点应该是

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

这是一个实际的例子或者是你寻求帮助做一个分配? :-)