Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Jeff Higgins Newsgroups: comp.lang.java.programmer Subject: Re: The greeting code in Java Date: Sun, 19 Jun 2011 13:07:08 -0400 Organization: A noiseless patient Spider Lines: 22 Message-ID: References: <3d6aab49-9a4a-4614-af12-bb1c95bebbe7@w4g2000yqm.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 19 Jun 2011 17:02:17 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="yplGUUrGzpiV4sv58weMcA"; logging-data="18462"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FBfNSDoh3J/boyMjOGHxJHQ5h9ALawI8=" User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 In-Reply-To: <3d6aab49-9a4a-4614-af12-bb1c95bebbe7@w4g2000yqm.googlegroups.com> Cancel-Lock: sha1:y7zSFvOZel/0YdD6i54PEekCjdA= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5389 On 06/19/2011 09:06 AM, Saeed Amrollahi wrote: > > 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. > besides what others have mentioned, the condition (ch = (char)(r.read())) != '\n' introduces a platform dependence.