Archived

This topic is now archived and is closed to further replies.

KurtCPP

Stupid question about java

Recommended Posts

KurtCPP    130
howdy. since im gonna have to learn java at university next year, i decided to begin right now, but i havent been able to find anywhere how i could perform a input-reading from the keyboard.though that looked so simple that I thought it would have been the first thing i would have found.can someone tell me why there''s no doc. about this?? Prog, Hex & Rock''n''Roll : I don''t like the Prog but the Prog likes me. Some nice poetry to sweeten your spirit and relax a bit before programming

Share this post


Link to post
Share on other sites
rohde    432
Keyboard input in Java is insanely hard! Use an input dialog instead.



"Yeah, I would''ve killed you, but I''m glad I didn''t - the paperwork is a bitch"

Share this post


Link to post
Share on other sites
justo    184
its a system dependent function, like the System.out.print for printing. it seems, for some reason i couldnt put my finger on when i was learnign java, that keyboard input in java would be some weird thing, but its actually pretty simple.

basically you make a bufferedReader object that takes System.in as its source. a great website explaining it is here:

http://www.csupomona.edu/~cs/labs/help_pages/docs/java/Keyboard_Input.htm

hope that helps.
justo

Share this post


Link to post
Share on other sites
justo    184
really not that hard. and let me correct myself. bufferedReader takes a Reader as an argument. you are simply turning System.in into a Reader and feeding it to the bufferedReader. its that easy. the beauty of it is, of course, is that you can also make Readers out of everything from files to Strings.

and welcome to java! my college forced me into it too. i dragged my feet all the way, but i love it now (lets avoid any kind of language debate though). if youre just starting (especially if you want to get a headstart on everyone like you seem to) i highly highly suggest O'Reilly's "Learning Java." i still use it as reference. your new best friend will become

http://java.sun.com/j2se/1.4.1/search.html

which has all the java docs, if you dont already have them on your comp, indexed and searchable.

and if youre interested in graphics, head on over to Ken Perlin's webpage, where he does a lot of his current research side projects in pure java 1.1 applets.

http://mrl.nyu.edu/~perlin/

he usually posts his source code, including double buffered applet frameworks, for his classes, but many of them are free to use if you keep the attribution and you do something original with them!

ok, went a little overboard, but i hope all that helps.
justo

[edited by - justo on August 13, 2003 7:45:12 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster

//simple method to grab input from the keyboard

public static String inputStr(String s)
{
String aLine = "";
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print(s);

try
{
aLine = input.readLine();
}

catch (Exception e)
{
e.printStackTrace();
}

return aLine;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by KurtCPP
howdy.
since im gonna have to learn java at university next year, i decided to begin right now, but i havent been able to find anywhere how i could perform a input-reading from the keyboard.though that looked so simple that I thought it would have been the first thing i would have found.can someone tell me why there''s no doc. about this??

Prog, Hex & Rock''n''Roll :
I don''t like the Prog but the Prog likes me.
<a href=http://perso.numericable.fr/guivyvon/>Some nice poetry to sweeten your spirit and relax a bit before programming</a>


If you mean console IO that can be done in a myriad of ways. Checkout <a href="http://java.sun.com/tutorial/">this</a> for a start. If you mean actual key-press based input then just make a JPane and implement your own handlers for key press events.

Share this post


Link to post
Share on other sites
bastard2k5    238
Usually, at least at the school I am going to, they don''t get into java keyboard input until second term, because it requires you to be able to handle exceptions, like the source example in a previous post had. If you are learning it just for fun, just grab the very basics and jump straight into something you would think would be fun, because it will be a while before you will have a fun project to work on again.

Share this post


Link to post
Share on other sites
Kentaro    122
My school does console input but they''ve written a class and methods to do the work and exception-handling for us. They provide it along with some other classes created for file IO and for learning basic OO concepts.

Over the centuries, mankind has tried many ways of combating the forces of evil...prayer,
fasting, good works and so on. Up until Doom, no one seemed to have thought about the
double-barrel shotgun. Eat leaden death, demon...
-- Terry Pratchett

Share this post


Link to post
Share on other sites
KurtCPP    130
Hi you all !!
I had promised to myself that I would no longer reply my own posts only in order to thank people since I had notices that it''s useful only to bring the post back to the top needlessly and thus to hide the most recent posts, but though today, since you all gave me a serious help (and even a joke also !) I still wanted to tell you all "Merci beaucoup" for your guidance.
I hope the other guys at university dont know GameDev so that I''ll be much stronger than them. Yeeeaaaaaaaaaaaaahhhh !!!
Voila. Long life to GameDev.

Prog, Hex & Rock''n''Roll :
I don''t like the Prog but the Prog likes me.
Some nice poetry to sweeten your spirit and relax a bit before programming

Share this post


Link to post
Share on other sites