Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Something about this while statement that I can't seem to figure out

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Something about this while statement that I can't seem to figure out

    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.
    Last edited by Straitsfan; February 6th, 2022 at 09:40 PM. Reason: needed to format code samples

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Something about this while statement that I can't seem to figure out

    A while loop exits when its boolean expression is false.
    When are all the conditions tested in the while loop false?
    The OR operator means that if any one of the sub expressions is true the expression is true.
    (false || false || false || true) is true

    With the AND operator, all the sub expressions need to be true for the full expression to be true:
    (true && true && true && false) is false

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    --- Update ---

    Also posted here: https://coderanch.com/t/749462/java/...operly#3480334

    Please read: http://www.javaprogrammingforums.com...s-posting.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2024
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Something about this while statement that I can't seem to figure out

    while ((!contactName.equals("STOP"))||(!address1.equals( "STOP"))||
    (!address2.equals("STOP"))||(!state.equals("STOP") )||
    (!zipCode.equals("STOP"))||(!phoneNumber.equals("S TOP"))||
    (!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

    Do you want to come out of the whole while loop or just the add contact method when you type STOP.

    while ((!contactName.equals("STOP"))||(!address1.equals( "STOP"))||
    (!address2.equals("STOP"))||(!state.equals("STOP") )||
    (!zipCode.equals("STOP"))||(!phoneNumber.equals("S TOP"))||
    (!email.equals("STOP")) ) {.....} the contact name or address even if you type stop , it wont come out of the loop as per your logic here.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Something about this while statement that I can't seem to figure out

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2024
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Re: Something about this while statement that I can't seem to figure out

    When you enter "Stop" in your input console, as per the logic that you have now, it will not come out of the loop. Because inside the while loop you are just making entries and dont have any checks to come out of the loop when you give input as "Stop".
    So the below variables are updated with value that you provide as inputs inside the while loop during one iteration:
    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();

    Now before going for next iteration, the below expression is being checked:
    while ((!contactName.equals("STOP"))||(!address1.equals( "STOP"))||
    (!address2.equals("STOP"))||(!state.equals("STOP") )||
    (!zipCode.equals("STOP"))||(!phoneNumber.equals("S TOP"))||
    (!email.equals("STOP")) )

    for which you have STOP as input, so the condition become false and the loop doesn't go for next iteration here.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Something about this while statement that I can't seem to figure out

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: December 10th, 2019, 06:17 AM
  2. How to turn my If statement into a case/switch statement?
    By blobby404 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: June 19th, 2014, 03:11 PM
  3. Replies: 4
    Last Post: March 29th, 2014, 07:49 PM
  4. Random Boolean in an if statement (tricky, can't seem to figure out)
    By Xhalite in forum Loops & Control Statements
    Replies: 6
    Last Post: May 29th, 2013, 07:28 AM
  5. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM