Hello --
I want to preface this by saying I'm not a professional programmer. I really enjoy learning programming/Java though, and have some experience with programming going back to my high school days, but it's been a LOOOOONG time since going back to it (I'm in my 50's now....)
I'm trying to put together a basic contacts application myself. I thought it would be a good way to try to learn the language, gradually building it as I learn more and more (Eventually I'd like to put this on my Linux computer, rather than try to find an app online).
I have the Head First Java Book, and it's really a good one; clear explanations and good examples.
For now, I have a while statement, in the my MainMenu class, which has an addContact() method that takes in string information, and populates the variables of my Contact class, then adds it to a contactList ArrayList. Here's the code for the MainMenu class:
import java.util.*; public class MainMenu {// open class ArrayList<Contact>contactList=new ArrayList<Contact>(); MenuHelper helper= new MenuHelper(); public void showMenu(String mainMenuChoice) {//open method while (Integer.parseInt(mainMenuChoice)!=4) {//open while loop System.out.println("Main Menu:"); System.out.println(); System.out.println("1: Add Contact"); System.out.println("2: Edit/Delete Contact"); System.out.println("3: Search For Contact"); System.out.println("4: Quit Application"); System.out.println(); mainMenuChoice = helper.getUserInput("Enter Choice:"); if (Integer.parseInt(mainMenuChoice)==1) {//open if loop for choices addContact(); }//close if loop }//close while loop }//close showMenu() method public void addContact() {//open method Scanner myScanner=new Scanner(System.in); String contactName=""; String address1=""; String address2=""; String state=""; String city=""; String zipCode=""; String phoneNumber=""; String email=""; System.out.println(); System.out.println("Enter STOP to return to Main Menu"); System.out.println(); while ((!contactName.equals("STOP"))||(!address1.equals("STOP"))|| (!address2.equals("STOP"))||(!state.equals("STOP"))|| (!zipCode.equals("STOP"))||(!phoneNumber.equals("STOP"))|| (!email.equals("STOP")) ) { Contact contact=new Contact(); System.out.print("Contact name: "); contactName=myScanner.nextLine(); System.out.println(); System.out.print("Address 1: "); address1=myScanner.nextLine(); System.out.println(); System.out.print("Address 2: "); address2=myScanner.nextLine(); System.out.println(); System.out.print("City: "); city=myScanner.nextLine(); System.out.println(); System.out.print("State: "); state=myScanner.nextLine(); System.out.println(); System.out.print("Zip Code: "); zipCode=myScanner.nextLine(); System.out.println(); System.out.print("Phone Number: "); phoneNumber=myScanner.nextLine(); System.out.println(); System.out.print("Email: "); email=myScanner.nextLine(); System.out.println(); contact.setName(contactName); contact.setAddress1(address1); contact.setAddress2(address2); contact.setState(state); contact.setCity(city); contact.setZip(zipCode); contactList.add(contact); System.out.println(); System.out.println(contactName); System.out.println(contactList.get(0).getName()); }//close while }//close method }//close class
Here's a sample output:
Main Menu: 1: Add Contact 2: Edit/Delete Contact 3: Search For Contact 4: Quit Application Enter Choice:1 Enter STOP to return to Main Menu Contact name: Name Address 1: Street Address 2: Street2 City: STOP State: STOP Zip Code: STOP Phone Number: STOP Email: STOP Name Name Contact name: Name 2 Address 1: Street2 Address 2: Street City: STOP State: STOP Zip Code: STOP Phone Number: STOP Email: STOP Name 2 Name Contact name:
For the life of me I can't seem to figure why the while statement won't break out of the loop even after I entered the first contact -- at the very least it should break out of the loop after the first contact, when the "STOP" condition is met when it goes back to the top of the "while" loop, but it doesn't.
It's probably right in front of my face, yet I can't see why.
So -- is these even a case where a WHILE loop is even appropriate? But it's the only thing that can continuously loop to enter a theoretically limitless number of contacts to the ArrayList. So do I need to set some other condition in the while loop to get it to loop, and then an individual IF statement for each instance variable to see if "STOP" has been entered, then break out of the loop?
If you're wondering about the two input methods I'm using, I came across the Scanner() method on another blog/site, and thought I'd use it. The other method in the showMenu() method is some ready bake code from one of the HFJ chapters.
The println() methods at the end of while loop are there for me, as a way of trying to see what's going on. I don't plan on keeping them.
And, as I said before, please forgive any mistakes in here re: coding etiquette/standards. I'm still trying to learn them, based on what I've read in the code examples so far in the HFJ book, but I may have made mistakes, as well as the possibility that the code isn't as lean as it could be.
Any advice will be welcome.