Hi, am new to java and have been given the following exercise. Can someone help me out:
'Write an application that prompts the user for their first name and then their last name. The application should then respond with 'Hello first & last name, what is the weather like today? Once the user has answered, the application should reply with 'Maybe tomorrow will be nicer'.
Related posts:




3 responses so far ↓
1 Ravi Chandra // Apr 27, 2008
For this level of user interaction in Java, you need to use buffered I/O streams. You can look this up on google where you can get a lot of examples…
2 Jackson // Apr 27, 2008
import java.io.*;
public class Simple {
/**
* @param args
*/
public static void main(String[] args)
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(isr);
String line = "";
String prompt = ("Input your first and last name, use "
+ "the Enter key after each name is input:\n");
System.out.println(prompt);
String strFirst = "";
String strLast = "";
try
{
strFirst = input.readLine();
strLast = input.readLine();
System.out.println( "Hello " + strFirst + " " + strLast + ", what is the weather like today?");
input.readLine();
System.out.println( "Maybe tomorrow will be nicer.");
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
3 coding // Apr 27, 2008
before you dive into stream chains and buffers, I recommend you to first consider java.util.Scanner class for taking simple input. For a beginner it is much easier to understand Scanner concept, rather than streams and buffers.
e.g.:
import java.util.Scanner;
public class Interact{
public static void main(String[]args){
String first ="", last="";
Scanner sc = new Scanner(System.in);
try{
System.out.println("Enter your first name, press enter to confirm");
first = sc.nextLine();
System.out.println("Enter your last name, press enter to confirm");
last = sc.nextLine();
}catch(Exception e){}
System.out.format("Hello %1$s %2$s, what is the wheather like today?", first, last);
}
}
this is not complete example but you'll get it finished…
Leave a Comment