如何在字符串中查找int,uppercase,lowercase,space和digits

时间:2014-09-24 09:52:26

标签: java

已经解决了,感谢所有帮助!!

我是java语言的新手,我一直坚持这个问题很长一段时间......每当我执行我的代码时,它会编译但是当我运行它并输入一个字符串(任何字符串)时,我得到这个错误:

  

java.lang.StirngIndexOutOfBoundsException:Stirng索引超出范围:   在java.lang.String.charAt(未知来源)at   Reading.main(Reading.java:48)

有人可以帮帮我吗?这是我的代码:

import java.awt.*;  
import javax.swing.*;
import java.util.*;

public class Reading{
    static Scanner input = new Scanner(System.in);
    public static void main(String [] args){
        System.out.println("Enter a string of characters: ");   
        int i, Upper=0, Lower=0, Space=0, Digits=0;
        String answer = input.nextLine();
        for(i = 0; i <= answer.length(); i++);
        {
            if(Character.isUpperCase(answer.charAt(i)))
                Upper++;
            if(Character.isLowerCase(answer.charAt(i)))
                Lower++;
            if(Character.isDigit(answer.charAt(i))) 
                Digits++;
        if (answer.charAt(i)==' ')
            Space++;
            System.out.println("There are " + Upper + " upper case letters");
            System.out.println("There are " + Lower + " lower case letters");
            System.out.println("There are " + Digits + " digits");
            System.out.println("There are " + Space + " spaces");
        }
        System.exit(0);
    }
} 

3 个答案:

答案 0 :(得分:1)

你必须改变

for (i = 0; i <= answer.length(); i++) ;

for (i = 0; i <answer.length(); i++) ;

answer.charAt(answer.length())没有匹配,不仅如此

您需要删除

for(i = 0; i <= answer.length(); i++); <==remove ;

然后你的for循环应该是这样的

for (i = 0; i < answer.length(); i++) {
     if (Character.isUpperCase(answer.charAt(i)))
         Upper++;
     if (Character.isLowerCase(answer.charAt(i)))
         Lower++;
     if (Character.isDigit(answer.charAt(i)))
         Digits++;
     if (i == ' ') // i is an index this should be if(answer.charAt(i)==' ')
         Space++;
 }

答案 1 :(得分:0)

这是一个错误的错误。

更改

for(i = 0; i <= answer.length(); i++);

for(i = 0; i < answer.length(); i++);

答案 2 :(得分:0)

因为索引超出answer索引而超出范围错误。数组从0开始,到array.length()-1结束,因此您需要更改代码并移除=中的<=,然后它将迭代到length-1

对于空间计数器,您需要将answer的字符与空格进行比较,这意味着您需要将if(i == ' ')替换为if(answer.charAt(i) == ' ')

这是你想要的:

import java.awt.*;
import javax.swing.*;
import java.util.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    static Scanner input = new Scanner(System.in);
    public static void main(String [] args)
    {
        System.out.println("Enter a string of characters: ");   
        int i, Upper=0, Lower=0, Space=0, Digits=0;
        String answer = input.nextLine();
        System.out.println("Enter a string of characters2: ");
        for(i = 0; i < answer.length(); i++);
        {
            if(Character.isUpperCase(answer.charAt(i)))
                Upper++;

            if(Character.isLowerCase(answer.charAt(i)))
                Lower++;

            if(Character.isDigit(answer.charAt(i))) 
                Digits++;

            if (answer.charAt(i)==' ')
            Space++;

             System.out.println("There are " + Upper + " upper case letters");
             System.out.println("There are " + Lower + " lower case letters");
             System.out.println("There are " + Digits + " digits");
             System.out.println("There are " + Space + " spaces");
        }
        System.exit(0);
    }

}