Do while loop problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saytri
    New Member
    • Dec 2007
    • 35

    Do while loop problem

    I have a problem with do while loop. I am displaying a dialogue box where a person should enter the type of quiz he had performed. The problem is that the user should enter only the following 3 words, either "Earthquake s" or "Place" or "Animals". If the user enters any other word, he/she should be displayed wwith another message saying "Invalid". This should keep repeatinf (i.e. displaying invalid) until the user enters either of those 3 words. When a user enters one of those words it will stop diplaying "Invalid". I tried doing this, but altough it displays "Invalid" when a user enters an invalid word, it will display invalid also when a user neters the correct word. I think i have something wrong with the loop.

    This is the code:

    Code:
      String player = JOptionPane.showInputDialog(null, "Enter name");
                        
                 String type = JOptionPane.showInputDialog(null, "Enter your quiz type"); 
                 
                do
                {
                    type = JOptionPane.showInputDialog(null, "Invalid entry. Try again." ); 
                }
                
                 while (type != "Earthquakes"  || type != "Place" || type != "Animals" );
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by saytri
    I have a problem with do while loop. I am displaying a dialogue box where a person should enter the type of quiz he had performed. The problem is that the user should enter only the following 3 words, either "Earthquake s" or "Place" or "Animals". If the user enters any other word, he/she should be displayed wwith another message saying "Invalid". This should keep repeatinf (i.e. displaying invalid) until the user enters either of those 3 words. When a user enters one of those words it will stop diplaying "Invalid". I tried doing this, but altough it displays "Invalid" when a user enters an invalid word, it will display invalid also when a user neters the correct word. I think i have something wrong with the loop.

    This is the code:

    Code:
      String player = JOptionPane.showInputDialog(null, "Enter name");
                        
                 String type = JOptionPane.showInputDialog(null, "Enter your quiz type"); 
                 
                do
                {
                    type = JOptionPane.showInputDialog(null, "Invalid entry. Try again." ); 
                }
                
                 while (type != "Earthquakes"  || type != "Place" || type != "Animals" );
    The first thing that you need to change/know is that you should use the .equals method for comparing strings in Java.

    Comment

    • saytri
      New Member
      • Dec 2007
      • 35

      #3
      Ok i took your advice, but still it keeps displaying "Invalid" even when entering the correct word.

      Code:
      do {
      
          type = JOptionPane.showInputDialog(null, "Invalid entry. Try again." );
      
      } while(!type.equals("Earthquakes") || !type.equals("Place") || !type.equals("Animals"));

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        You just need to learn logic. Consider your condition:

        [CODE=Java]!type.equals("E arthquakes") || !type.equals("P lace") || !type.equals("A nimals")[/CODE]

        You loop as long as it is true. It is a disjunction of three clauses so you loop as long any of these clauses is true:

        !type.equals("E arthquakes")
        !type.equals("P lace")
        !type.equals("A nimals")

        Wait a minute -- any string would make at least one of those true! Consider another string, say "Java" -- it makes all three clauses true. But then one of listed words, say "Animals", makes the the first and second clause true, so the whole disjunction is true. Thus the full expression is true for any non-null string (and throws a NullPointerExce ption for a null string, so don't go there!).

        Comment

        • Dimpol
          New Member
          • Jul 2010
          • 1

          #5
          String inputs;
          do{
          inputs = JOptionPane.sho wInputDialog(nu ll,"Enter number:");

          }
          while(!inputs.e quals("Earthqua kes" ) && !inputs.equals( "Animals") && !inputs.equals( "Place"));
          }

          ~you should not use "||" .. Use "&&".. :D

          Comment

          Working...