Question: In the example code provided, we will begin learning file output and reading. We will start with file output. Why is file output important, you
In the example code provided, we will begin learning file output and reading. We will start with file output. Why is file output important, you ask? Because with the ability to input and output data into/from a program, you can begin writing programs to process multiple sets of data and make recordings.
Begin by analyzing the structure of the main method. First, a keyboard scanner is called so a user can input their personalized data. Next, prompts tell what data a user should enter. Afterwards, a PrintStream is called, which will allow the user to write to an output file. Finally, you close the PrintStream and the program ends. Take note of the import statements used and the exception thrown in the main method.Now it's your turn. Modify this code to add features related to file I/O, modify an old program to use files in a cool way, or create something new: How would you save progress in a video game? How would you write or load personalized settings into a program or user interface? How do you verify phone numbers or email addresses?
import java.util.Scanner; import java.io.PrintStream; import java.io.IOException; public class CreateContact{ private static boolean firstWrite = true; public static void main(String[] args) throws IOException{ //create variables for user-information String firstName; String lastName; String phoneNumber; Scanner keyboardReader = new Scanner(System.in); System.out.println("What would you like to name your file?"); String outFileName = keyboardReader.nextLine(); //output a CSV file PrintStream fileStream = new PrintStream( outFileName + ".csv"); do{ firstName = getName("First", keyboardReader); lastName = getName("Last", keyboardReader); phoneNumber = getNumber("10 digit Phone Number,", 10, keyboardReader); write(firstName, lastName, phoneNumber, fileStream); }while(moreContacts(keyboardReader)); fileStream.close(); } //End Main public static String getName(String name, Scanner kbReader) { String readName = null; while(readName == null) { System.out.println("Please type your contacts " + name + " name, then 'enter': "); readName = kbReader.nextLine(); } return readName; }// End getName public static String getNumber(String number, int digit, Scanner kbReader) { String readNumber = null; String numbers = "0123456789"; Boolean notNumber = false; do{ notNumber = false; System.out.println("Please type your contacts " + number + " then 'enter': "); readNumber = kbReader.nextLine(); if((readNumber.length() < digit) || (readNumber.length() > digit)) { System.out.println("I am sorry, I seem to have an incorrect number of digits."); System.out.println("Please type your contacts " + number + " then 'enter': "); readNumber = kbReader.nextLine(); } else // readNumber.length() == 10 { for(int i = 0; i < digit; i++) { if(!Character.isDigit(readNumber.charAt(i))) notNumber = true; } } if(notNumber) readNumber = null; } while(readNumber == null); return readNumber; }// End getNumber public static void write(String nameOne, String nameTwo, String number, PrintStream fileStream) { if(firstWrite) { fileStream.print("First Name,"); fileStream.print("Last Name,"); fileStream.println("Phone Number"); firstWrite = false; } fileStream.print(nameOne + ","); fileStream.print(nameTwo + ","); fileStream.println(number); //fileStream.println("=\"" + Number +"\""); // For an Excel only CSV file }// End write public static boolean moreContacts(Scanner kbReader) { System.out.println("More contacts? (Enter y or yes)"); String more = kbReader.nextLine(); if((more.equalsIgnoreCase("y")) || (more.equalsIgnoreCase("yes"))) { return true; } else { return false; } }// End moreContacts }// End CreatContact Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
