Sunday, September 10, 2017

Data Input

Challenge: Write a program that: a) asks the user to enter his/her email address and his/her postal address, b) stores the email address in email, a String variable, and the postal address in postalAddress, a String variable, c) concatenates the values of email and the value of postalAddress, and assign the result to output, a String variable, d) prints the value of output.


Solution:



import java.util.*;

class lab2_1{
   
public static void main(String []args)
{




String email;
String postalAddress;
System.out.println("Please enter your email address and your postal address");
System.out.println("Enter your email:");
Scanner in=new Scanner(System.in);
email=in.nextLine();
System.out.println("Enter your postal address");
postalAddress=in.nextLine();
String output="The email address entered is: "+email+" and the postal address: "+postalAddress;
System.out.println(output);
   }
}


Our first step to complete this program will be declaring two variables to hold our email and postal address value (email and postalAddress).  Since both variables are of type String that’s the variable type we are going to use and we are going to name then accordingly to the values asked in the instruction (email and postalAddress). Then we are going to let the user know we need its email and postal address and we use the Scanner method so the user could type the info needed. After the info is entered, we concatenate both variable value and we print it out.

No comments:

Post a Comment