Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > The greeting code in Java

Reply
Thread Tools

The greeting code in Java

 
 
Stefan Ram
Guest
Posts: n/a
 
      06-19-2011
Jeff Higgins <> writes:
>What means simple?


10 INPUT "Please enter your first name: "; N$
20 PRINT "Hello, "; N$; "!"
30 END

 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      06-19-2011
On 06/19/2011 11:24 AM, Aéris wrote:
> Le 19/06/2011 17:06, Stanimir Stamenkov a écrit :
>> I guess one needs:
>>
>> System.out.print("Please enter your first name: ");
>> while ((s = in.readLine()) != null&& s.length()> 0) {
>> System.out.println("Hello, " + s);
>> System.out.print("Please enter your first name: ");
>> }
>>

>
> Shame on me…
> My finger slipped on Eclipse…
>

The original example didn't loop.

BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
StringBuilder builder = new StringBuilder("Hello, ");
System.out.print("Please enter your first name: ");
try {
builder.append(reader.readLine());
builder.append("!");
System.out.println(builder);
} catch (IOException e) {
System.err.println("Something's wrong, a stack trace follows:");
e.printStackTrace();
}
 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      06-19-2011
On 06/19/2011 11:24 AM, Aéris wrote:
>
> Shame on me…
> My finger slipped on Eclipse…
>

Pretty, slick, Eclipse.
 
Reply With Quote
 
Stanimir Stamenkov
Guest
Posts: n/a
 
      06-19-2011
Sun, 19 Jun 2011 06:06:17 -0700 (PDT), /Saeed Amrollahi/:

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


You don't need to initialize 'name' with a 'new String()' - just
assign it with an empty string literal which is a constant:

String name = "";

The 'name += ch' will assign it a new String instance, also. In
this regard you may better use a StringBuilder:

StringBuilder name = new StringBuilder();
Reader r = new InputStreamReader(System.in);
int ch = r.read();
while (ch != -1 && ch != '\n') {
name.append((char) ch);
ch = r.read();
}
System.out.println("Hello, " + name);

In the above snippet I also check whether the Reader is returning
-1, which would mean 'System.in' has been closed for some reason -
otherwise you risk going in infinite loop, in that case.

--
Stanimir
 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      06-19-2011
On 06/19/2011 11:36 AM, Stefan Ram wrote:
> Jeff Higgins<> writes:
>> What means simple?

>
> 10 INPUT "Please enter your first name: "; N$
> 20 PRINT "Hello, "; N$; "!"
> 30 END
>

I acquired my first PC at a yard sale in 1984.
It was an SBC 2000: monitor, single 5" floppy,
and keyboard all in one case.
I spent many enjoyable hours on that machine.
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      06-19-2011
Saeed Amrollahi <> writes:
>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;


public class Main
{
public static void main( final java.lang.String[] args )
{
java.lang.System.out.print( "Please enter your first name: " );
java.lang.System.out.flush();
java.lang.System.out.println
( "Hello, " +
new java.util.Scanner( java.lang.System.in ).nextLine() +
"!" ); }}

 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      06-19-2011
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.
 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      06-19-2011
On 06/19/2011 01:07 PM, Jeff Higgins wrote:
There is also java.io.Console
<http://download.oracle.com/javase/6/docs/api/java/io/Console.html>


 
Reply With Quote
 
Jeff Higgins
Guest
Posts: n/a
 
      06-19-2011
On 06/19/2011 01:11 PM, Jeff Higgins wrote:
> On 06/19/2011 01:07 PM, Jeff Higgins wrote:
> There is also java.io.Console
> <http://download.oracle.com/javase/6/docs/api/java/io/Console.html>
>
>

public class Main {
public static void main(String[] args) {
System.console().printf("Hello, %1$s!\n",
System.console().readLine("Please enter your first name: "));
}
}
 
Reply With Quote
 
Saeed Amrollahi
Guest
Posts: n/a
 
      06-19-2011
On Jun 19, 6:31*pm, Jeff Higgins <j...@invalid.invalid> wrote:
> On 06/19/2011 09:06 AM, Saeed Amrollahi wrote:
>
> > What are the problems of my code and how can I write
> > a better one. Please throw some light.

>
> What means better?


For example using less abstractions and less involve with Java
stream class hierarchy.
-- Saeed
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The greeting code in Java Saeed Amrollahi Java 15 07-24-2011 02:15 AM
Greeting cards application in java - what is the best approach? tak Java 0 02-27-2007 11:29 PM
Thanks to all - Eastern greeting - No Question Matthias S. ASP .Net 1 03-27-2005 02:21 PM
Url / Page Greeting? Leon ASP .Net 2 12-03-2004 02:47 PM
HTML problem with Nova Greeting Card Factory Sharon HTML 6 01-03-2004 03:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57