Skip to content

Instantly share code, notes, and snippets.

@keysRB
Created November 2, 2014 02:26
Show Gist options
  • Save keysRB/7c16b256e3bc01da6a5d to your computer and use it in GitHub Desktop.
Save keysRB/7c16b256e3bc01da6a5d to your computer and use it in GitHub Desktop.
Printing an envelope..How to pass variables?
//Purpose: Print a letter to yourself stating your name and address. Formatting should be included around info.
import java.util.Scanner;
public class LetterToYourself {
public static void main(String[] args) {
boolean exit = false;
do {
LetterFormat getInput = new LetterFormat();
getInput.getInput(); // Get user input
LetterFormat letterTop = new LetterFormat();
letterTop.printTop(); // Print the top half of the letter.
// *******Format name line with information to fit the box formatting: ********************
System.out.printf("|");
int totalSpace = (60 - charLength[0]); // Use the array to find out how long the input is
int spaceLeft = totalSpace / 2;
for (int j = 0; j<spaceLeft; j++ ) { //Need to count number of spaces to print before and after name
System.out.printf(" ");
}
System.out.printf(userInfo[0]);
for (int k = 0; k < spaceLeft; k++) {
System.out.printf(" ");
}
System.out.println("|");
// ****************************************************************************************
// *******Format address line with information to fit the box formatting: ********************
System.out.printf("|");
//int addressLength = userAddress.length(); // Find length of address
totalSpace = (60 - charLength[1]);
spaceLeft = totalSpace / 2;
for (int j = 0; j<spaceLeft; j++ ) {
System.out.printf(" ");
}
System.out.printf(userInfo[1]);
for (int k = 0; k < spaceLeft; k++) {
System.out.printf(" ");
}
System.out.println("|");
// ****************************************************************************************
// *******Format location line with information to fit the box formatting: ********************
System.out.printf("|");
totalSpace = (60 - charLength[2]);
spaceLeft = totalSpace / 2;
for (int j = 0; j<spaceLeft; j++ ) {
System.out.printf(" ");
}
System.out.printf(userInfo[2]);
for (int k = 0; k < spaceLeft; k++) {
System.out.printf(" ");
}
System.out.println("|");
// ****************************************************************************************
System.out.println("| |");
System.out.println(horizLine);
exit = true;
} while(exit == false);
}
}
import java.util.Scanner;
public class LetterFormat {
void getInput() {
Scanner userInput = new Scanner(System.in);
System.out.printf("Please enter your first name: ");
String firstName = userInput.nextLine();
System.out.printf("Please enter your last name:");
String lastName = userInput.nextLine();
System.out.printf("Please enter your address:");
String userAddress = userInput.nextLine();
System.out.printf("Please enter your City, State Zip Code:");
String userLocation = userInput.nextLine();
String horizLine = "+------------------------------------------------------------+";
String[] userInfo = {firstName + " " + lastName, userAddress, userLocation}; // Store input in an array
// Use an array to keep track of how many characters each input takes up.
int[] charLength = {firstName.length() + lastName.length() + 1, userAddress.length(), userLocation.length()};
// Added +1 to the first item for the space between the first and last name.
}
void printTop() {
for (int i = 0; i<1; i++ ){ //Print top half of envelope
System.out.println("+------------------------------------------------------------+"); //Print Top Format Line
System.out.println("| #### |");
System.out.println("| #### |");
System.out.println("| #### |");
System.out.println("| |");
System.out.println("| |");
}
}
void printInfo() {
// *******Format name line with information to fit the box formatting: ********************
System.out.printf("|");
int totalSpace = (60 - charLength[0]); // Use the array to find out how long the input is
int spaceLeft = totalSpace / 2;
for (int j = 0; j<spaceLeft; j++ ) { //Need to count number of spaces to print before and after name
System.out.printf(" ");
}
System.out.printf(userInfo[0]);
for (int k = 0; k < spaceLeft; k++) {
System.out.printf(" ");
}
System.out.println("|");
// ****************************************************************************************
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment