Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.java.programmer > #5399

Re: The greeting code in Java

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 <amrollahi.saeed@gmail.com>
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 <b70ab7d1-fe03-413e-ba87-6819ae24973e@hd10g2000vbb.googlegroups.com> (permalink)
References <f61fee62-589e-4ad1-a9ef-a54e2b589e5b@s9g2000yqm.googlegroups.com> <ld8sv6tugbkdq7n1dc0d4ja0o604rr6n5q@4ax.com>
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

Show key headers only | View raw


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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

The greeting code in Java Saeed Amrollahi <amrollahi.saeed@gmail.com> - 2011-06-19 06:05 -0700
  Re: The greeting code in Java rossum <rossum48@coldmail.com> - 2011-06-19 17:36 +0100
    Re: The greeting code in Java Saeed Amrollahi <amrollahi.saeed@gmail.com> - 2011-06-19 12:15 -0700
      Re: The greeting code in Java Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-19 19:46 +0000
        Re: The greeting code in Java Saeed Amrollahi <amrollahi.saeed@gmail.com> - 2011-06-19 22:34 -0700
          Re: The greeting code in Java Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2011-06-23 11:43 +0300
        Re: The greeting code in Java Martin Gregorie <martin@address-in-sig.invalid> - 2011-06-20 10:13 +0000
          Re: The greeting code in Java Michael Wojcik <mwojcik@newsguy.com> - 2011-06-22 09:29 -0400
      Re: The greeting code in Java Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-19 16:00 -0400
      Re: The greeting code in Java Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-19 16:05 -0400
        Re: The greeting code in Java Saeed Amrollahi <amrollahi.saeed@gmail.com> - 2011-06-19 22:40 -0700
      Re: The greeting code in Java rossum <rossum48@coldmail.com> - 2011-06-19 22:28 +0100
  Re: The greeting code in Java Roedy Green <see_website@mindprod.com.invalid> - 2011-06-19 21:14 -0700
  Re: The greeting code in Java Roedy Green <see_website@mindprod.com.invalid> - 2011-06-19 21:23 -0700
  Re: The greeting code in Java blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-20 19:20 +0000
    Re: The greeting code in Java Ney André de Mello Zunino <zunino@softplan.com.br> - 2011-06-22 15:55 -0300

csiph-web