Monday, 21st June 2010
The new frontier for learning Java

Greeting.java

From WikiJava

Jump to: navigation, search


This is a simple application that demonstrates how Input and Output is done in java programming.

Contents

the article

This is a simple application that prompts the user for thier first name and last name the application then responds with 'Hello first & last name, what is the weather like today?' once the user has answered, the application should re[ly with 'Maybe tomorrow will be nicer'.

input/output example

package org.wikijava.simpleIO;
 
/** 
 * @author (Alex Mutuku Mbolonzi)  
 * @version (07/06/08)
 */
 
import java.io.*;
 
public class Greeting {
 
    public static void main(String args[]) throws Exception {
 
	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
 
	String firstName = "";
	String lastName = "";
	String weather = "";
 
	// The system prompts the user to enter their First Name
 
	/**
	 * asks the first name until the user writes something
	 */
 
	while ("".equals(firstName)) {
	    System.out.print("PLEASE ENTER YOUR FIRST NAME: ");
	    firstName = in.readLine();
	}
 
	/**
	 * asks the last name until the user writes something
	 */
	while ("".equals(lastName)) {
	    System.out.print("PLEASE ENTER YOUR LAST NAME: ");
	    lastName = in.readLine();
	}
 
	/**
	 * Once the user has entered their first and last name the system
	 * displays a greeting massage in upper case and prompts the user to
	 * describe how the weather is like, the system then displays a massage
	 * telling the user that maybe the weather will be better tomorrow.
	 */
 
	System.out.print("HELLO " + firstName.toUpperCase() + "  "
		+ lastName.toUpperCase());
 
	System.out.print(",  What is the Weather like today? ");
 
	weather = in.readLine();
 
	System.out.print("Maybe tomorrow will be nicer.");
    }
 
}


Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page

Variable names and While loops

Thanks for sharing, I modified the names of your variables, it is good practice to avoid short names and acronyms. The results are more legible.

I also deleted the ifs using instead a while. There's no duplicate code now and the while prevents the Strings to be empty.

--DonGiulio 08:54, 17 September 2008 (PDT)

Comments and import

Nice :) It remembers me my very old days (actually 24 years ago) writing BASIC code on my C64 :P

I usually prefer to import just the right class, and avoid the .* import.

Anyway I think there are some good points in this class.

--Alcor 11:46, 17 September 2008 (PDT)

Title (required):

Website:

Comment: