Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   The greeting code in Java (http://www.velocityreviews.com/forums/t750157-the-greeting-code-in-java.html)

Saeed Amrollahi 06-19-2011 01:05 PM

The greeting code in Java
 
Dear all
Hi

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. The C++ code is:
#include <iostream>
#include <string>
Using std::cin; using std::cout; using std::string;
int main()
{
// ask for the person's name
std::cout << "Please enter your first name: ";
std::string name; // define name
std::cin >> name; // read into name
// write a greeting
std::cout << "Hello, " << name << "!" << std::endl;

return 0;
}
I tried to write the simplest code in Java and I ended up with the
following:

package Greeting;
import java.io.*;

public class Main {

public static void main(String[] args) throws IOException {
System.out.print("Please enter your first name: ");
String name = new String();
Reader r = new InputStreamReader(System.in);
for (char ch; (ch = (char)(r.read())) != '\n'; name += ch) {}
System.out.println("Hello, " + name);
}
}

What are the problems of my code and how can I write
a better one. Please throw some light.

TIA,
-- Saeed Amrollahi

Saeed Amrollahi 06-19-2011 07:15 PM

Re: The greeting code in Java
 
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:
> >Dear all
> >Hi

>
> >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. The C++ code is:
> >#include <iostream>
> >#include <string>
> >Using std::cin; * * using std::cout; * * * *using std::string;
> >int main()
> >{
> > *// ask for the person's name
> > *std::cout << "Please enter your first name: ";
> > *std::string name; // define name
> > *std::cin >> name; * // read into name
> > *// write a greeting
> > *std::cout << "Hello, " << name << "!" << std::endl;

>
> > *return 0;
> >}
> >I tried to write the simplest code in Java and I ended up with the
> >following:

>
> >package Greeting;
> >import java.io.*;

>
> >public class Main {

>
> > * *public static void main(String[] args) throws IOException {
> > * * * *System.out.print("Please enter your first name: ");
> > * * * *String name = new String();
> > * * * *Reader r = new InputStreamReader(System.in);
> > * * * *for (char ch; (ch = (char)(r.read())) != '\n'; name += ch) {}
> > * * * *System.out.println("Hello, " + name);
> > * *}
> >}

>
> >What are the problems of my code and how can I write
> >a better one. Please throw some light.

>
> >TIA,
> > *-- Saeed Amrollahi

>
> 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);
> * }
>
> rossum


What is the Scanner? Why we use nextLine? 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!
-- Saeed
for

Martin Gregorie 06-19-2011 07:46 PM

Re: The greeting code in Java
 
On Sun, 19 Jun 2011 12:15:01 -0700, Saeed Amrollahi wrote:

> What is the Scanner? Why we use nextLine? 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 sounds as though you didn't download and install the Java SE
documentation package. You need it: the Java equivalent of the C standard
library is considerably bigger and is fully documented there via the
Javadocs documentation tool, which is also part of the standard JDK.
C/C++ doesn't have a standard documentation tool that comes near
Javadocs.

The Java 6 standard class library includes the Scanner class, so of
course it is fully described in the Java 6 documentation set. The
description there is much better than any summary we might write. Quite
apart from anything else, you'll find that writing your comparison is a
lot easier if you have the documentation set installed and use it to help
your writing.


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |

Joshua Cranmer 06-19-2011 08:00 PM

Re: The greeting code in Java
 
On 06/19/2011 03:15 PM, Saeed Amrollahi wrote:
> What is the Scanner? Why we use nextLine? What's the relation of
> such concepts with a simple greeting program.


java.util.Scanner is a very useful class for parsing simple textual
protocols, that makes retrieval of words, lines, numbers, etc. very
simple. In other words, it's very much like istream::operator<< except
more explicit in what it is doing (does operator<<(string&) feed me a
word or a line?); that Java did not have it until version 5 is a great
shame.

> 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!


Outputting ASCII is very simple, and "Hello, world" has become (for
better or for worse) the standard introduction to programming. True I/O,
one that takes into account the harsh vagaries of international text and
the heaping mess that is character sets, is actually very difficult and
very easy to get wrong. Reading from an input stream is actually
logically difficult (what happens if you read from a closed stdin?);
Java 1.0 actually got this mildly wrong, which is part of the reason why
the APIs for particularly reading Strings is much more circuitous.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Eric Sosman 06-19-2011 08:05 PM

Re: The greeting code in Java
 
On 6/19/2011 3:15 PM, Saeed Amrollahi wrote:
> [...]
> What is the Scanner? Why we use nextLine? What's the relation of
> such concepts with a simple greeting program.


The Scanner class and its nextLine method are part of an I/O
library that offers more power and flexibility than your program is
capable of using.

Unfortunately for you, the designers of Java chose not to
provide a built-in I/O facility dumbed down to the level you need.
You're not using a tenth of the power of Scanner, but that would
be a poor reason for Scanner to jettison its other nine-tenths.

> 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!


Permute the chapter numbers if it makes you happier. For example,
you don't need to know how to write constructors, you don't need to
understand the `long' type, you don't need to know about nested classes,
and so on, and so on. Move chapter 20 ahead of all those, if you like.

More seriously, the purpose of a "Hello, world" program is not
to teach you the language (whatever language) nor to illustrate its
capabilities. It is a throat-clearing exercise intended to test
whether the compiler/interpreter/libraries/runtime/licenses/whatnot
are set up correctly. "Testing, testing, one, two, three" is not
intended to convey a message, but to check that everything from the
microphone to the speaker is properly connected and powered on. Do
not judge the PA system by the banality of its first message.

I imagine you are a C++ practitioner trying to learn Java, but
making the mistake of trying to understand Java in C++'s terms. Have
you ever hear the phrase "A real programmer can write FORTRAN in any
language?" Try not to commit that error, but instead look at Java on
its own merits (and its own demerits; Java's not perfect).

Bruce Eckel's "Thinking in Java" gets a lukewarm reception (at
best) from the experts, and I don't know whether it's been kept up
to date as Java has developed and changed. However, it has one virtue
I found helpful Back In The Day, and you may find helpful now: True to
its title, it really does try to explain Java in Java's own terms, and
not by saying "It's just like Lisp except ..." A goodly dose of that
mindset might stand you in good stead.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Stefan Ram 06-19-2011 08:24 PM

Re: The greeting code in Java
 
Martin Gregorie <martin@address-in-sig.invalid> writes:
>C/C++ doesn't have a standard documentation tool that comes near
>Javadocs.


Classical C was part of UNIX, and its »standard
documentation tool« was »(n|g)roff -man«/»man«.

However, it has not been man portable together with C,
while JavaDoc is part of the Java SE. This portability
and standardization of JavaDoc is its greatest advantage.


Roedy Green 06-20-2011 04:14 AM

Re: The greeting code in Java
 
On Sun, 19 Jun 2011 06:05:53 -0700 (PDT), Saeed Amrollahi
<amrollahi.saeed@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>What are the problems of my code and how can I write
>a better one. Please throw some light.


see http://mindprod.com/applet/fileio.html
to get it to generate code for various purposes.

You can read a line at a time and save yourself a fair bit of
complication.
--
Roedy Green Canadian Mind Products
http://mindprod.com
One of the great annoyances in programming derives from the irregularity
of English spelling especially when you have international teams.
I want to find a method or variable, but I don't know precisely
how its is spelled or worded. English is only approximately phonetic.
Letters are randomly doubled. The dictionary often lists variant spellings.
British, Canadian and American spellings differ.I would like to see an
experiment where variable names were spelled in a simplified English, where
there were no double letters.I also think you could add a number of rules
about composing variable names so that a variable name for something would
be highly predictable. You would also need automated enforcement of the
rules as well as possible.

Roedy Green 06-20-2011 04:23 AM

Re: The greeting code in Java
 
On Sun, 19 Jun 2011 06:05:53 -0700 (PDT), Saeed Amrollahi
<amrollahi.saeed@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>package Greeting;
>import java.io.*;
>
>public class Main {


packages are traditionally all lower case. e.g. "greeting".

Further they should be globally unique by including your domain name,
e.g. "com.amrollahi.saeed.greeting". That way you can share code with
others without worrying about package name clashes.

Main is not a very descriptive name for a class. Try "Greeting".

The code itself works fine, though it is a "vortope" translation of C
rather than idiomatic Java. You forgot to close your stream.

An fairly easy extension is a while EOFException loop to handle more
than one name.

Thank you for compiling and running your code before asking help. So
many newbies fail to do that. You also explained what your code was
supposed to do. These are lessons that seem almost impossible to get
into newbie heads.

--
Roedy Green Canadian Mind Products
http://mindprod.com
One of the great annoyances in programming derives from the irregularity
of English spelling especially when you have international teams.
I want to find a method or variable, but I don't know precisely
how its is spelled or worded. English is only approximately phonetic.
Letters are randomly doubled. The dictionary often lists variant spellings.
British, Canadian and American spellings differ.I would like to see an
experiment where variable names were spelled in a simplified English, where
there were no double letters.I also think you could add a number of rules
about composing variable names so that a variable name for something would
be highly predictable. You would also need automated enforcement of the
rules as well as possible.

Saeed Amrollahi 06-20-2011 05:34 AM

Re: The greeting code in Java
 
On Jun 19, 10:46*pm, Martin Gregorie <mar...@address-in-sig.invalid>
wrote:
> On Sun, 19 Jun 2011 12:15:01 -0700, Saeed Amrollahi wrote:
> > What is the Scanner? Why we use nextLine? 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 sounds as though you didn't download and install the Java SE
> documentation package. You need it: the Java equivalent of the C standard
> library is considerably bigger and is fully documented there via the
> Javadocs documentation tool, which is also part of the standard JDK.
> C/C++ doesn't have a standard documentation tool that comes near
> Javadocs.
>
> The Java 6 standard class library includes the Scanner class, so of
> course it is fully described in the Java 6 documentation set. The
> description there is much better than any summary we might write. Quite
> apart from anything else, you'll find that writing your comparison is a
> lot easier if you have the documentation set installed and use it to help
> your writing.
>
> --
> martin@ * | Martin Gregorie
> gregorie. | Essex, UK
> org * * * |


I'll download it and use it.
-- Saeed Amrollahi

Saeed Amrollahi 06-20-2011 05:40 AM

Re: The greeting code in Java
 
On Jun 19, 11:05*pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> On 6/19/2011 3:15 PM, Saeed Amrollahi wrote:
>
> > [...]
> > What is the Scanner? Why we use nextLine? What's the relation of
> > such concepts with a simple greeting program.

>
> * * *The Scanner class and its nextLine method are part of an I/O
> library that offers more power and flexibility than your program is
> capable of using.
>
> * * *Unfortunately for you, the designers of Java chose not to
> provide a built-in I/O facility dumbed down to the level you need.
> You're not using a tenth of the power of Scanner, but that would
> be a poor reason for Scanner to jettison its other nine-tenths.
>
> > 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!

>
> * * *Permute the chapter numbers if it makes you happier. *For example,
> you don't need to know how to write constructors, you don't need to
> understand the `long' type, you don't need to know about nested classes,
> and so on, and so on. *Move chapter 20 ahead of all those, if you like.
>
> * * *More seriously, the purpose of a "Hello, world" program is not
> to teach you the language (whatever language) nor to illustrate its
> capabilities. *It is a throat-clearing exercise intended to test
> whether the compiler/interpreter/libraries/runtime/licenses/whatnot
> are set up correctly. *"Testing, testing, one, two, three" is not
> intended to convey a message, but to check that everything from the
> microphone to the speaker is properly connected and powered on. *Do
> not judge the PA system by the banality of its first message.
>
> * * *I imagine you are a C++ practitioner trying to learn Java, but
> making the mistake of trying to understand Java in C++'s terms. *Have
> you ever hear the phrase "A real programmer can write FORTRAN in any
> language?" *Try not to commit that error, but instead look at Java on
> its own merits (and its own demerits; Java's not perfect).
>
> * * *Bruce Eckel's "Thinking in Java" gets a lukewarm reception (at
> best) from the experts, and I don't know whether it's been kept up
> to date as Java has developed and changed. *However, it has one virtue
> I found helpful Back In The Day, and you may find helpful now: True to
> its title, it really does try to explain Java in Java's own terms, and
> not by saying "It's just like Lisp except ..." *A goodly dose of that
> mindset might stand you in good stead.
>

Of course I don't do that. Each programming language has its own
terminology,
culture, ideals, idioms and styles.
-- Saeed Amrollahi
> --
> Eric Sosman
> esos...@ieee-dot-org.invalid




All times are GMT. The time now is 05:28 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.