New To Java - null
I am trying to write a program that reads contacts from a file and also writes contacts to a file. For some reason when I select the option to read from a file, I get a runtime error. When I select the option to write to a file, It asks for all user input correctly and also writes to a file correctly, but does not go through my do while loop (which is supposed to ask if I would like to return to the main menu). Instead it just sits there after 'Data written to the file.' appears on my console window. What can I do to correct these errors?
Here is my code:
import java.util.Scanner;// Needed for the Scanner class
import java.io.*;// Needed for file classes
publicclass random2{
publicstaticvoid main(String[] args)throws IOException{
// Create a Scanner object for keyboard input.
Scanner keyboard =new Scanner(System.in);
int repeat = 0;
do{
System.out.println("Please select one of the following contact manager options: ");
System.out.println();
System.out.println();
System.out.println("Enter [1] to: Read contacts from an existing file.");
System.out.println("Enter [2] to: Add contacts to a new or existing file.");
System.out.println("Enter [0] to: End the program.");
System.out.println();
System.out.println();
System.out.println();
System.out.print("Which option would you prefer? ");
int selection = keyboard.nextInt();
if (selection == 1){
String filename;// File name
String contact;// Contacts information
// Get the filename.
System.out.print("Enter the name of the file that you would like to view contacts from: ");
filename = keyboard.nextLine();
// Open the file.
FileReader freader =new FileReader(filename);
BufferedReader inputFile =new BufferedReader(freader);
// Read the first name from the file.
contact = inputFile.readLine();
// If a contact was read, display it and
// read the remaining names.
while (contact !=null){
// Display the last name read.
System.out.println(contact);
// Read the next name.
contact = inputFile.readLine();
}
// Close the file.
inputFile.close();
}
elseif (selection == 2){
String filename;// File name
String surName;
String firstName;
String organization;
String phoneNumber;
String emailAddress;
int contacts;// Number of contacts
// Get the number of contacts.
System.out.print("How many new contacts would you like to add? ");
contacts = keyboard.nextInt();
// Consume the remaining newline character.
keyboard.nextLine();
// Get the filename.
System.out.print("Enter the name of the file that you would like to add your contacts to: ");
filename = keyboard.nextLine();
// Open the file.
FileWriter fwriter =new FileWriter(filename,true);
PrintWriter outputFile =new PrintWriter(fwriter);
// Get data and write it to the file.
for (int i = 1; i <= contacts; i++){
// Get the surname of the contact.
System.out.print("Enter the surname of contact " +
"number " + i +": ");
surName = keyboard.nextLine();
// Get the first name of the contact.
System.out.print("Enter the name of contact " +
"number " + i +": ");
firstName = keyboard.nextLine();
// Get the name of the organization of the contact.
System.out.print("Enter the name of the organization of contact " +
"number " + i +": ");
organization = keyboard.nextLine();
// Get the phone number of the contact.
System.out.print("Enter the phone number of contact " +
"number " + i +": ");
phoneNumber = keyboard.nextLine();
// Get the e-mail address of the contact.
System.out.print("Enter the e-mail address of contact " +
"number " + i +": ");
emailAddress = keyboard.nextLine();
// Write the name to the file.
outputFile.println(surName +"\t" + firstName +"\t" + organization
+"\t" + phoneNumber +"\t" + emailAddress);
}
// Close the file.
outputFile.close();
System.out.println("Data written to the file.");
}
elseif (selection == 0){
System.exit(0);
}
else{
System.out.println("Error: The number you had entered was not 1, 2, 3, or 0");
System.exit(0);
}
keyboard.nextLine();
System.out.println("Would you like to go back to the main menu? ");
System.out.print("Please enter [0] for No and enter [1] for Yes: ");
repeat = keyboard.nextInt();
}while (repeat == 1);
}
}

