java范围索引_数组索引超出范围异常(Java)

这是我的代码:

public class countChar {

public static void main(String[] args) {

int i;

String userInput = new String();

userInput = Input.getString("Please enter a sentence");

int[] total = totalChars(userInput.toLowerCase());

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

{

if (total[i] != 0) {

System.out.println("Letter" + (char) ('a' + i) + " count =" + total[i]);

}

}

}

public static int[] totalChars(String userInput) {

int[] total = new int[26];

int i;

for (i = 0; i < userInput.length(); i++) {

if (Character.isLetter(userInput.charAt(i))) {

total[userInput.charAt(i) - 'a']++;

}

}

return total;

}

}

程序的目的是向用户询问字符串,然后计算字符串中每个字符的使用次数.

当我去编译程序时,它工作正常.当我运行程序时,我能够在弹出框中输入一个字符串,但在我提交字符串并按OK后,我收到错误,说

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 26

at countChar.main(countChar.java:14)

我不完全确定问题是什么或如何解决它.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值