Results 1 to 5 of 5
  1. #1
    vividcooper is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Cool comparing 3 strings with equalsignorecase

    hi i m trying to compare the input of the user with the specified strings but the program is continue telling me to re-enter the status again.
    this is my code:

    Java Code:
    import java.util.*;
    
    class test{
    
    public static void main(String args[]){
    
    Scanner sc = new Scanner(System.in);
    //ENTERING THE CLIENT'S STATUS AND VALIDATING IT
    System.out.print("Enter client's status (single,married,committed): ");
    
    
    String st;
    st = sc.next();
    
    do{
    
    //checking that the user input is correct ignoring whether the input is lower or upper case
    if(!st.equalsIgnoreCase("single") || !st.equalsIgnoreCase("married") || !st.equalsIgnoreCase("committed"))
    
    
      System.out.println("Status does not exist!! Re-enter status again:");
      st = sc.next();
                                
    }while(!st.equalsIgnoreCase("single") || !st.equalsIgnoreCase("married") || !st.equalsIgnoreCase("committed"));
                                        }}
    thanks and will be glad if i'll find help
    Last edited by Fubarable; 01-13-2010 at 08:56 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I added code tags to help make your code more readable. Please have a look at my signature below to see how to do this yourself next go around.

    OK, back to your problem... to be blunt, your formatting is pretty bad, and I think that it is this formatting that is partly killing you. Remember to enclose all if blocks, while blocks, for loops, etc... in curly braces.

    For example, it looks as if both the println and sc.next() statements below are within the if block, but only the first one is:
    Java Code:
    if(!st.equalsIgnoreCase("single") || !st.equalsIgnoreCase("married") || !st.equalsIgnoreCase("committed"))
      System.out.println("Status does not exist!! Re-enter status again:");
      st = sc.next();
    The real cause of your problem though is your logic, and to solve this, you're going to have to mentally walk through your code. Imagine what is going to happen in your while statement if someone enters the correct String. Think this through and you'll find your solution.

  3. #3
    vividcooper is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    yes yes yoy have a point but as I tried this:

    if(!st.equalsIgnoreCase("single") || !st.equalsIgnoreCase("married") || !st.equalsIgnoreCase("committed")){


    System.out.println("Status does not exist!! Re-enter status again:");
    st = sc.next();}

    it still haven't worked so I thought it also works without the curly brackets :S

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's still your logic. Will the while loop every end? Will you ever have a String entered were this is false? (not "A" OR not "B" OR not "C")?

    or even more simply, will this ever by false: (not A OR not B)?

    Think on it.

  5. #5
    vividcooper is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    ok I think I find the solution...

    I should have write && instead of ||
    thanks

Similar Threads

  1. need in help in comparing Strings
    By jaq in forum New To Java
    Replies: 1
    Last Post: 11-25-2009, 01:06 PM
  2. homework help comparing strings
    By djester55 in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 10:15 PM
  3. comparing strings
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 12-23-2008, 04:40 AM
  4. Comparing Strings
    By souFrag in forum Advanced Java
    Replies: 5
    Last Post: 05-21-2008, 09:03 AM
  5. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •