Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Incorrect Key Input

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Incorrect Key Input

    Hi the program I made plays sound files when the user input a numeric key. How do I get the program to identify when any key other than ‘1’, ‘2’, ‘3’, ‘4’ as incorrect keys and displays an error message when the user presses a wrong key. Ive attached the sound files. Thanks

    import java.awt.*;
    import java.applet.*
     
    public class PlaySoundAndMusic extends Applet{
       Image Buffer;
       Graphics gBuffer;
     
       AudioClip clip;
       AudioClip clip2;
       AudioClip clip3;
       AudioClip clip4;
     
       public void init(){
     
         Buffer=createImage(size().width,size().height);
          gBuffer=Buffer.getGraphics();
     
          try
          {
               clip=getAudioClip(getCodeBase(),"gong.wav");
               clip2=getAudioClip(getCodeBase(),"shoot.wav");
               clip3=getAudioClip(getCodeBase(), "bsg.mid");
               clip4=getAudioClip(getCodeBase(), "drwhotheme.mid");
     
     
     
     
    }
          catch (Exception e){}
       }
     
       public boolean keyDown(Event e, int key){
     
          if(key=='1')
               clip.play();
          if(key=='2')
               clip2.play();
          if(key=='3')
          		clip3.play();
          if(key=='4')
          		clip4.play();
     
     
          repaint();
     
          return true;
       }
     
     
       public void draw()
       {
          gBuffer.setColor(Color.WHITE);
          gBuffer.fillRect(0,0,size().width,size().height);
     
          gBuffer.setFont(new Font("Arial",Font.PLAIN,14));
          gBuffer.setColor(Color.RED);
          //if we want to use "" inside a string, we have to mask it with \!
          gBuffer.drawString ("Press numeric key ‘1’ to play gong.wav,",50,30);
          gBuffer.drawString ("Press numeric key '2' to play shoot.wav,",45,50);
          gBuffer.drawString ("Press numberic key '3' to play bsg.mid," , 40,70);
          gBuffer.drawString ("Press numberic key '4' to play drwhotheme.mid,", 35,90);
     
     
       }
     
       public void update(Graphics g)
       {
          paint(g);
       }
     
       public void paint(Graphics g)
       {
          draw();
          g.drawImage (Buffer,0,0, this);
       }
    }
    Attached Files Attached Files


  2. #2
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: Incorrect Key Input

    if(key=='1')

    You are comparing an int to a char. Remove the single-quotes and compare key to an int instead.

    Also what error message are you getting?

    EDIT: keyDown is a deprecated method. I strongly suggest you take a look at KeyListener.
    Last edited by OutputStream; June 28th, 2011 at 10:09 AM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Incorrect Key Input

    To see what is passed to the keyDown() method add a println to print it out. The results might not be what you expect.

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Incorrect Key Input

    Quote Originally Posted by OutputStream View Post
    if(key=='1')

    You are comparing an int to a char. Remove the single-quotes and compare key to an int instead.

    Also what error message are you getting?

    EDIT: keyDown is a deprecated method. I strongly suggest you take a look at KeyListener.
    I kind of new to writing programs. How do you use keylistener? I need to get finished with this program today

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Incorrect Key Input

    Go to this site: The Really Big Index
    and search for Key Listener

  6. The Following User Says Thank You to Norm For This Useful Post:

    risen375 (June 28th, 2011)

  7. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Incorrect Key Input

    Thank for the information. . How do I get the program to identify when any key other than ‘1’, ‘2’, ‘3’, ‘4’ as incorrect keys and displays an error message when the user presses a wrong key.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Incorrect Key Input

    Depends on how you are receiving the key presses.
    One way is with the if/else if/else structure you almost coded earlier. The trailing else clause would be executed if none of the preceding if tests were true.
    If the key value has the int value in the range: 1-4 then test if it's less than 1 or greater than 4.
    Or you could use a switch statement with the case statements for 1-4 all falling into one block of code and the default case being for the error.

  9. #8
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default Re: Incorrect Key Input

    Did you follow Norm's link and did you have a look at the tutorial? It explains a lot on how to use a KeyListener.

Similar Threads

  1. Replies: 10
    Last Post: April 5th, 2011, 09:09 AM
  2. [SOLVED] Code is correct, but incorrect output?
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2011, 02:32 PM
  3. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM
  4. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM
  5. Swing incorrect behaviour from JRE5 to JRE6
    By singhkanhaiya in forum AWT / Java Swing
    Replies: 1
    Last Post: August 25th, 2009, 01:23 AM