Re: Please recommend a book
On 10/29/2012 12:09 PM, bobwhite@mixnym.net wrote:
I don't think that's true because I'm familiar with abstract data types. My
confusion starts when I say gee let me read a string from the keyboard. Now
where do I go from there? I look at the Java API doc and try to find
something that looks like console IO. Ok, I see a function there, but now
how do I use it? I have to set up exception handling before I can do that!
How do I do that? And it goes downhill from there.
Some of this is just "time," like I mentioned before. Java has a BIG
API and you won't learn it well by just reading one thing. Getting a
few basic books and looking at how they do it will give you some ideas.
In other words, there's "patterns" here that work, and some that don't.
Also, the API is spread out. It's been improved incrementally for 17
odd years, and similar things are not all together. In general to read
user input you want the System, not Console. Console is recent and just
contains some extensions to the basic I/O that people were asking for.
System.in is the workhorse. Wrap that in a BufferedReader and read from
that.
Time and just plowing through will get you there after a while. The
important bit is to do it.
Here's the most basic method. Try to build on this, even if fancier
methods are available, until you get more familiar with the Java API and
things start to be easier. Note that for quick-and-dirty and testing I
think it's better to NOT catch the exception, just declare the main
method with " throws Exception" and let the system do its thing.
<http://www.mkyong.com/java/how-to-read-input-from-console-java/>
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadConsoleSystem {
public static void main(String[] args) {
System.out.println("Enter something here : ");
try{
BufferedReader bufferRead = new BufferedReader(new
InputStreamReader(System.in));
String s = bufferRead.readLine();
System.out.println(s);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}