S
Saeed Amrollahi
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
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