Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: blmblm@myrealbox.com Newsgroups: comp.lang.java.programmer Subject: Re: The greeting code in Java Date: 20 Jun 2011 19:20:45 GMT Organization: None Lines: 55 Message-ID: <969kscFjduU6@mid.individual.net> References: X-Trace: individual.net wq05KfaD0bWKfCp69NHLlAW4pdtoklNBgwvdj3oAxFVsSkpHoF X-Orig-Path: not-for-mail Cancel-Lock: sha1:ulFUolXgLmzDf53u5Gq7KrVKlqU= X-Newsreader: trn 4.0-test76 (Apr 2, 2001) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5429 In article , Saeed Amrollahi 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 > #include > 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); > } > } Um, has anyone else pointed out that these two programs don't do the same thing? Unless the C++ implemented by GCC is broken, the C++ program reads a whitespace-delimited string, while the Java program reads a full line of text. Just sayin'. (I agree by the way with most of the other comments about there being much better ways to accomplish -- well, whatever it is you're trying to accomplish, given that the two programs seem to me to be doing slightly different things.) -- B. L. Massingill ObDisclaimer: I don't speak for my employers; they return the favor.