Ok, it looks like I've ran into a few problems with my main class. It breaks my loop on the 2nd run and displays and error. It says my scanner to read the user selection from the menu is creating the error? How is that, it worked the first loop, but for some reason it can't run again.
"action = new Scanner(System.in).nextInt();"
is generating the error. Does anyone know why this is happening, as it is very important to read the integer the user puts in as the user is selecting a menu option.
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.String;
import java.lang.System;
public class MainActions {
public static void main(String args[]) throws IOException {
int action = 0;
while (action != 6) {
Contact.menu();
new Scanner(System.in);
action = new Scanner(System.in).nextInt();
if (action <= 0 || action > 6) {
System.out.println("Invalid selection. ");
}
switch (action) {
case 1:
MainActions.addContactInfo();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
}
}
public static void addContactInfo() {
Contact contact;
contact = new Contact();
Scanner reader = new Scanner(System.in);
System.out.println("Enter Contact Last Name:");
String lastname = reader.nextLine();
contact.setLastName(lastname);
System.out.println("Enter Contact First Name: ");
contact.setFirstName(reader.nextLine());
System.out.println("Enter Contact Street Address: ");
contact.setHouseAddress(reader.nextLine());
System.out.println("Enter Contact City: ");
contact.setCity(reader.nextLine());
System.out.println("Enter Contact Zip Code: ");
contact.setZip(reader.nextLine());
System.out.println("Enter Contact Email: ");
contact.setEmail(reader.nextLine());
System.out.println("Enter Contact Phone Number: ");
contact.setPhone(reader.nextLine());
System.out.println("Enter Contact Notes: ");
contact.setNotes(reader.nextLine());
if (lastname.trim().equals("")) {
System.out.println("\nLast name was blank, contact not saved.");
System.exit(0);
} else {
ContactList list;
list = new ContactList();
list.add(contact);
list.save();
Contact c = contact;
try (PrintWriter output = new PrintWriter(new FileWriter("contactlist.csv", true))) {
output.printf("%s\r\n", c);
} catch (Exception e) {}
}
reader.close();
}
}
Console:
1. Enter a new person
2. Print the contact list
3. Retrieve a person's information by last name
4. Retrieve a person's information by email address
5. Retrieve all people who live in a given zip code
6. Exit
1
Enter Contact Last Name:
asdf
Enter Contact First Name:
asdf
Enter Contact Street Address:
asdf
Enter Contact City:
asdf
Enter Contact Zip Code:
asdf
Enter Contact Email:
asdf
Enter Contact Phone Number:
asdf
Enter Contact Notes:
asdf
Contact information has been saved.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at MainActions.main(MainActions.java:33)
1. Enter a new person
2. Print the contact list
3. Retrieve a person's information by last name
4. Retrieve a person's information by email address
5. Retrieve all people who live in a given zip code
6. Exit