【问题标题】:String out of bounds,字符串越界,
【发布时间】:2011-12-03 00:14:43
【问题描述】:

这个程序是使用键盘按键来弹奏音符。对于我按下的每个键,我都会得到一个超出范围的不同字符串索引,范围从 1 的 49 到 m 的 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;
                }
        }
        }
    }
}

【问题讨论】:

  • key 被调用时的值是多少? String keyboard 共有 37 个字符,字符占用索引 0-36。因此,如果密钥 > 大于 36 或 IndexOutOfBoundsException

标签: java string indexing


【解决方案1】:

你想打电话给String#indexOf(int),它会给你角色的索引。 String#charAt(int) 返回给定索引处的字符。

【讨论】:

    【解决方案2】:

    你需要indexOf方法

    返回此字符串中指定字符第一次出现的索引

    而不是charAt

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

    【讨论】:

      【解决方案3】:

      问题出在: 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;
          }
      }
      

      【讨论】:

      • @bitva 很高兴它成功了。以供将来参考并帮助您的其他成员找到正确的答案,如果它对您有用,请接受此帖子作为答案:)
      猜你喜欢
      • 2014-07-31
      • 2023-03-14
      • 2016-03-01
      • 2012-12-01
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 2018-09-11
      相关资源
      最近更新 更多