字符串越界,

时间:2011-12-03 00:14:43

标签: java string indexing

此程序是使用键盘键来播放音符。对于我按下的每个键,我得到一个不同的字符串索引,范围从49到1到109。但我总是收到此错误消息。我是Java的新手,任何帮助都会受到赞赏,因为我已经检查了很多论坛,并且没有找到解决这类问题的答案。

此行抛出异常:

nextnote = keyboard.charAt(key);

这是我的代码:

public class GuitarHero {
    public static void main(String[] args) {
        //make array for strings
        double[] notes = new double[37];
        GuitarString[] strings = new GuitarString[37];
        int nextnote;
        int firstnote=0;
        double NOTE = 440.0;
        String keyboard ="1234567890qwertyuiopasdfghjklzxcvbnm";
        //for loop to set notes
        for(int i=0;i<37;i++){
            double concert = 440.0* Math.pow(2, (i-24)/12.0);
            notes[i] = concert;
            for(int j=0;j<37;j++){
                strings[j] = new GuitarString(concert);
            }
        }
        while (true) {
            // check if the user has typed a key; if so, process it
            if (StdDraw.hasNextKeyTyped()) {
                char key = StdDraw.nextKeyTyped();
                //charAt gets index of character in string 
                nextnote = keyboard.charAt(key);
                //make sure value is within string
                if(nextnote>=0 && nextnote<37){
                    // pluck string and compute the superposition of samples
                strings[nextnote].pluck();
                    double sample = strings[firstnote].sample() 
                          +strings[nextnote].sample();
                    StdAudio.play(sample);
                    // advance the simulation of each guitar string by one step   
                    strings[nextnote].tic();
                    firstnote=nextnote;
                }
        }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

您想要调用String#indexOf(int),它会为您提供角色的索引。 String#charAt(int)返回给定索引处的字符。

答案 1 :(得分:1)

您需要indexOf方法

  

返回第一次出现的指定字符

的字符串中的索引

而不是charAt

  

返回指定索引处的char值。索引的范围从0到length() - 1.序列的第一个char值在索引0处,下一个在索引1处,依此类推,就像数组索引一样。

答案 2 :(得分:1)

问题在于: StdDraw.nextKeyTyped();文档说:

  

用户键入的下一个键是什么?这个方法返回一个   与键入的键对应的Unicode字符(例如“a”或“A”)。   它无法识别操作键(例如F1和箭头键)或修饰符   键(如控件)。

key是一个字符,而不是此行的索引。请改为:

int charIndexInKeyboard = keyboard.indexOf(key);
if(charIndexInKeyboard == -1) // char not recognized
nextnote = keyboard.charAt(charIndexInKeyboard );

nextnote现在应该包含您想要的角色。

编辑:以下是您的while循环现在的样子

while (true) {
    // check if the user has typed a key; if so, process it
    if (StdDraw.hasNextKeyTyped()) {
        char key = StdDraw.nextKeyTyped();
        int charIndexInKeyboard = keyboard.indexOf(key);
        if(charIndexInKeyboard == -1){
            // Not recognized, just continue to next
            continue;
        }
        nextnote = keyboard.charAt(charIndexInKeyboard);
        // pluck string and compute the superposition of samples
        strings[nextnote].pluck();
        double sample = strings[firstnote].sample()
        +strings[nextnote].sample();
        StdAudio.play(sample);
        // advance the simulation of each guitar string by one step
        strings[nextnote].tic();
        firstnote=nextnote;
    }
}
相关问题