Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Input from keyboard?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Unicornzrcool




PostPosted: Wed Apr 15, 2009 4:25 pm   Post subject: Input from keyboard?

*For Ready To Program Java* (I know it sucks but we have to use it in our school >.<)

Ok, I'm making a menu where the user presses any key to move to the first page, how would you make it so that instead of having to write the line and press enter, it will just go once you press the key?

Thanks! Very Happy
Sponsor
Sponsor
Sponsor
sponsor
Unicornzrcool




PostPosted: Thu Apr 16, 2009 7:12 am   Post subject: Re: Input from keyboard?

anyone..?
SJ




PostPosted: Thu Apr 16, 2009 7:42 pm   Post subject: Re: Input from keyboard?

haha i rbmr stuggling with this when i first started java

Java:
import java.io.IOException;

...

try {
            System.in.read();
            System.out.println ("Got it!");
} catch (IOException e) {
            e.printStackTrace();
}

...


note though, it's better practice to add "throws IOException" at the end of the method containing the read and get rid of the try catch block.

Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="java"]Code Here[/syntax]
Unicornzrcool




PostPosted: Fri Apr 17, 2009 12:32 am   Post subject: RE:Input from keyboard?

Although I'm not sure i did it right, it doesn't seem to be working.

What I did was put the import statement where the others were:

Java:
import java.awt.*;
import hsa.Console;
import java.io.IOException;


and put the other code where right after it says press any key to continue:

Java:
c.println ("Press any key to go back to the menu.");
                try
                {
                    System.in.read ();
                    System.out.println ("Got it!");
                }
                catch (IOException e)
                {
                    e.printStackTrace ();
                }

And what happens is when I go to the page where it says press any key, it opens a blank console window.

Am I putting the code in wrong?

Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="java"]Code Here[/syntax]
SJ




PostPosted: Fri Apr 17, 2009 7:01 am   Post subject: Re: Input from keyboard?

you are using the custom builtin console (the c) in the hsa package for "Ready to Program" and I gave you the code for a standard java System console.
the blank window you saw is the System console.
in your case, you can just use

Java:
c.getChar ();
HellblazerX




PostPosted: Fri Apr 17, 2009 9:42 am   Post subject: Re: Input from keyboard?

Since you want a keystroke to turn the page rather than the user inputting in something and pressing enter, you'll probably want to take a took at KeyListeners. Here's a tutorial for it. However, I'm not sure if KeyListeners will work with hsa.Console classes, as I've never used them myself.
Unicornzrcool




PostPosted: Fri Apr 17, 2009 12:28 pm   Post subject: RE:Input from keyboard?

Gah, I cant believe I've been looking around for days for such a small piece of code =P
Thank you so much guys!
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: