Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #6493
| Date | 2011-07-23 22:15 -0400 |
|---|---|
| From | Arne Vajhøj <arne@vajhoej.dk> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: The greeting code in Java |
| References | <f61fee62-589e-4ad1-a9ef-a54e2b589e5b@s9g2000yqm.googlegroups.com> <ld8sv6tugbkdq7n1dc0d4ja0o604rr6n5q@4ax.com> <b70ab7d1-fe03-413e-ba87-6819ae24973e@hd10g2000vbb.googlegroups.com> |
| Message-ID | <4e2b8058$0$316$14726298@news.sunsite.dk> (permalink) |
| Organization | SunSITE.dk - Supporting Open source |
On 6/19/2011 3:15 PM, Saeed Amrollahi wrote:
> On Jun 19, 8:36 pm, rossum<rossu...@coldmail.com> wrote:
>> On Sun, 19 Jun 2011 06:05:53 -0700 (PDT), Saeed Amrollahi
>> <amrollahi.sa...@gmail.com> wrote:
>>> I'm a C++ programmer and I started to learn Java. After famous "Hello
>>> World"
>>> program, the obvious code is "Say hello to specific people". Program
>>> asked
>>> user's name, then print a greeting message.
>> Stream readers are more often used for binary input. For text input
>> people tend to use the java.util.Scanner class.
>>
>> public static void main(String[] args) {
>> System.out.print("Please enter your first name: ");
>> Scanner sc = new Scanner(System.in);
>> String name = sc.nextLine();
>> System.out.println("Hello, " + name);
>> }
>
> What is the Scanner?
Something that scan's - in the same meaning as scanf.
> Why we use nextLine?
Some text plus hitting the return key is called a line,
so nextLine describes pretty well what it does.
> What's the relation of
> such concepts with a simple greeting program.
> Why the code for writing "Hello, world" is in chapter 1, page 1
> of The Java Programming Language, but the code of greeting may be in
> Chapter 20!
It should not be.
Simple use of Scanner should be in one of the first chapters.
If it is not then it can be because the book is old.
Scanner is a late invention.
Before Scanner:
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
would be done as:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine();
But that is not easier.
Also note that the Java API is pretty big.
Java 1.6 has approx. 3000 classes with approx. 100000
methods.
No book can cover everything.
So it is essential that you learn to find things in the
Java API docs.
Arne
Back to comp.lang.java.programmer | Previous | Next | Find similar
Re: The greeting code in Java Arne Vajhøj <arne@vajhoej.dk> - 2011-07-23 22:15 -0400
csiph-web