Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!news.ripco.com!news.glorb.com!postnews.google.com!hd10g2000vbb.googlegroups.com!not-for-mail From: Saeed Amrollahi Newsgroups: comp.lang.java.programmer Subject: Re: The greeting code in Java Date: Sun, 19 Jun 2011 12:15:01 -0700 (PDT) Organization: http://groups.google.com Lines: 72 Message-ID: References: NNTP-Posting-Host: 188.34.82.250 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1308511806 11770 127.0.0.1 (19 Jun 2011 19:30:06 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 19 Jun 2011 19:30:06 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: hd10g2000vbb.googlegroups.com; posting-host=188.34.82.250; posting-account=x4Lj4AoAAABzOb3wqfl972VDu6OmtJ9j User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HUALESNKRC X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110420 Firefox/3.6.17,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5399 On Jun 19, 8:36=A0pm, rossum wrote: > On Sun, 19 Jun 2011 06:05:53 -0700 (PDT), 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; =A0 =A0 using std::cout; =A0 =A0 =A0 =A0using std::strin= g; > >int main() > >{ > > =A0// ask for the person's name > > =A0std::cout << "Please enter your first name: "; > > =A0std::string name; // define name > > =A0std::cin >> name; =A0 // read into name > > =A0// write a greeting > > =A0std::cout << "Hello, " << name << "!" << std::endl; > > > =A0return 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 { > > > =A0 =A0public static void main(String[] args) throws IOException { > > =A0 =A0 =A0 =A0System.out.print("Please enter your first name: "); > > =A0 =A0 =A0 =A0String name =3D new String(); > > =A0 =A0 =A0 =A0Reader r =3D new InputStreamReader(System.in); > > =A0 =A0 =A0 =A0for (char ch; (ch =3D (char)(r.read())) !=3D '\n'; name = +=3D ch) {} > > =A0 =A0 =A0 =A0System.out.println("Hello, " + name); > > =A0 =A0} > >} > > >What are the problems of my code and how can I write > >a better one. Please throw some light. > > >TIA, > > =A0-- Saeed Amrollahi > > Stream readers are more often used for binary input. =A0For text input > people tend to use the java.util.Scanner class. > > =A0 public static void main(String[] args) { > =A0 =A0 System.out.print("Please enter your first name: "); > =A0 =A0 Scanner sc =3D new Scanner(System.in); > =A0 =A0 String name =3D sc.nextLine(); > =A0 =A0 System.out.println("Hello, " + name); > =A0 } > > 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