Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6890309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:15:56+00:00 2026-05-27T06:15:56+00:00

This program is to use the keyboard keys to play notes. I get a

  • 0

This program is to use the keyboard keys to play notes. I get a different string index out of range for each key I press, ranging from 49 for the 1 to 109 for the m. but I always get this error message. I am new to Java, and any help would be appreciated since I’ve checked a bunch of forums and haven’t found the answer to quite this kind of problem.

The exception is thrown at this line:

nextnote = keyboard.charAt(key);

This is my code:

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;
                }
        }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:15:56+00:00Added an answer on May 27, 2026 at 6:15 am

    You want to call String#indexOf(int), which will give you the index of the character. String#charAt(int) returns the character at the given index.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program I use has it's own variables to set when you run it,
Im trying to use Date Formatters (NSDateFormatter), but I keep getting this error: Program
use this website a lot but first time posting. My program creates a number
I use this to hide keyboard when i click done button or outside of
I'm creating a program that installs a keyboard hook to capture all keys and
This program stores pairs in a map, counting the number of times a word
This program reads emails (really just a .txt file structured like an email) and
This program I'm doing is about a social network, which means there are users
This program is meant to generate a dynamic array, however it gives an access
For this program #include <iostream> using std::cout; struct C { C() { cout <<

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.