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 24 of 24

Thread: How to turn my If statement into a case/switch statement?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question How to turn my If statement into a case/switch statement?

    So from what iv learnt in Java and programming in general is that using a case statement is far more efficient that using multiple IF statements. I have an multiple IF statements contained within a method of my program, and would like to instead use a case statement. Unfortunately, i do not know how. Please, rather than just give me an example of random code; could you please adapt my example (even if only by a couple of lines), that way, it is much easier for me to understand as i already grasp the concept of this method that i am using.

     
    public String checkPasswordStrength(String passw) 
        {
                    int strengthCount=0;
                    String strengthWord = "";
            String[] partialRegexChecks = { ".*[a-z]+.*", // lower
                                            ".*[A-Z]+.*", // upper
                                            ".*[\\d]+.*", // digits
                                            ".*[@#$%!]+.*" // symbols
            };
                if (passw.matches(partialRegexChecks[0])) 
                {
                        strengthCount+=1;
                }
     
                if (passw.matches(partialRegexChecks[1])) 
                {
                        strengthCount+=1;
                }
     
                if (passw.matches(partialRegexChecks[2])) 
                {
                        strengthCount+=1;
                }
     
                if (passw.matches(partialRegexChecks[3])) 
                {
                        strengthCount+=1;
                }
     
                if (passw.length()>7)
                {
                    strengthCount+=1;
                }
     
                //StrengthCount Converted to string
     
                if (strengthCount==0)
                {
                    strengthWord = "No password; da fuq hacks";
     
                }
     
                if (strengthCount==1)
                {
                    strengthWord = "Very Weak";
     
                }
     
                if (strengthCount==2)
                {
                    strengthWord = "Weak";
     
                }
     
                if (strengthCount==3)
                {
                    strengthWord = "Medium";
     
                }
     
                if (strengthCount==4)
                {
                    strengthWord = "Strong";
     
                }
     
                if (strengthCount==5)
                {
                    strengthWord = "Very Strong";
     
                }
     
                return strengthWord;
        }

    Please help me!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    I'm not sure I agree with your premise in general, but I definitely can see from your code that not all 'if' statements can be converted to more efficient switch statements. If you can suggest how that might be done in this case, I'd be interested to hear it.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Ahhhh, being fairly new to programming i assumed that i could make this code more efficient. So is there no way this code can be made more efficient?

    Kind Regards, Shaun

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    You could define an array of strength strings, like:

    String[] strengthString = { "No password; da fuq hacks",
                                               "Very Weak",
                                               "Weak",
                                                 . . . etc.
    and then return the appropriate strengthString using the strengthCount:

    return strengthString[strengthCount];

    to eliminate the last 6 if statements.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Would it be possible to show me the full code of how to do this? I'm not too sure of how to code this properly - even with the example you have given. I would be very grateful!

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    By the way, switch-case statements can only be used for variables of type int, char or Enum's.
    If-Then-Else blocks like these:
    if (passw.matches(partialRegexChecks[0]))
    Can not be made into a switch-case block.

    But comparisons like these:
    if (strengthCount==0)
    can be.

    An example would be:
    switch (strengthCount){
      case 0:
        // do stuff
        break;
      case 1:
        // do other stuff
        break;
      ...
    }

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Quote Originally Posted by blobby404 View Post
    Would it be possible to show me the full code of how to do this? I'm not too sure of how to code this properly - even with the example you have given. I would be very grateful!
    You try it and come back to ask for help with whatever problems you're having. I've shown you all but for ~3 lines of code.

  8. #8
    Junior Member
    Join Date
    Jun 2014
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Iv attempted this, and whilst it works to some degree; It is detecting Very strong passwords as strong, and strong passwords as meduim, etc. Help me please! Haha xD

    Below is my case statement which i have created.

     
       switch (strengthCount){
                case 0:
                strengthWord = "No password; da fuq hacks";
                 break;
                case 1:
                strengthWord = "Very Weak";
                break;
                case 2:
                strengthWord = "Very Weak";
                break;
                case 3:
                strengthWord = "Weak";
                break;
                case 4:
                strengthWord = "Medium";
                break;
                case 5:
                strengthWord = "Strong";
                break;
            }
                return strengthWord;

  9. #9
    Junior Member
    Join Date
    Jun 2014
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question IF statement TO Case statement issue

    I have created an algorithm which checks a password for its strength. The program does this by checking for length of string, and types of charecters used.

    I have created this method using mainly IF statements. I have tried to convert the IF statement into a Case statement; as this is more efficient. Although the Case statement still judges the strength of a password, it judges it wrong, judging very strong passwords as strong passwords, and strong passwords as meduim passwords etc.

    Below is the code for my working program using IF statements, and the second batch of code is the code which contains the case statement.

     
         public String checkPasswordStrength(String passw) 
        {
                int strengthCount=0;
                String strengthWord = "";
        String[] partialRegexChecks = { ".*[a-z]+.*", // lower
                                        ".*[A-Z]+.*", // upper
                                        ".*[\\d]+.*", // digits
                                        ".*[@#$%!]+.*" // symbols
        };
            if (passw.matches(partialRegexChecks[0])) 
            {
                    strengthCount+=1;
            }
     
            if (passw.matches(partialRegexChecks[1])) 
            {
                    strengthCount+=1;
            }
     
            if (passw.matches(partialRegexChecks[2])) 
            {
                    strengthCount+=1;
            }
     
            if (passw.matches(partialRegexChecks[3])) 
            {
                    strengthCount+=1;
            }
     
            if (passw.length()>7)
            {
                strengthCount+=1;
            }
     
            //StrengthCount Converted to string
     
            if (strengthCount==0)
            {
                strengthWord = "No password; da fuq hacks";
     
            }
     
    //ORIGINAL IF STATEMENT
     
            if (strengthCount==1)
            {
                strengthWord = "Very Weak";
     
            }
     
            if (strengthCount==2)
            {
                strengthWord = "Weak";
     
            }
     
            if (strengthCount==3)
            {
                strengthWord = "Medium";
     
            }
     
            if (strengthCount==4)
            {
                strengthWord = "Strong";
     
            }
     
            if (strengthCount==5)
            {
                strengthWord = "Very Strong";
     
            }
     
            return strengthWord;
    }

    Below is the complete code using the case statement.

     
       public String checkPasswordStrength(String passw) 
        {
                    int strengthCount=0;
                    String strengthWord = "";
            String[] partialRegexChecks = { ".*[a-z]+.*", // lower
                                            ".*[A-Z]+.*", // upper
                                            ".*[\\d]+.*", // digits
                                            ".*[@#$%!]+.*" // symbols
            };
                if (passw.matches(partialRegexChecks[0])) 
                {
                        strengthCount+=1;
                }
     
                if (passw.matches(partialRegexChecks[1])) 
                {
                        strengthCount+=1;
                }
     
                if (passw.matches(partialRegexChecks[2])) 
                {
                        strengthCount+=1;
                }
     
                if (passw.matches(partialRegexChecks[3])) 
                {
                        strengthCount+=1;
                }
     
                if (passw.length()>7)
                {
                    strengthCount+=1;
                }
     
                //StrengthCount Converted to string - CASE STATEMENT
     
                switch (strengthCount){
                case 0:
                strengthWord = "No password; da fuq hacks";
                 break;
                case 1:
                strengthWord = "Very Weak";
                break;
                case 2:
                strengthWord = "Very Weak";
                break;
                case 3:
                strengthWord = "Weak";
                break;
                case 4:
                strengthWord = "Medium";
                break;
                case 5:
                strengthWord = "Strong";
                break;
            }
                return strengthWord;
        }

    Any help would be greatly appreciated.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Threads merged. Please don't start new threads on the same topic.
    Case statement still judges the strength of a password, it judges it wrong
    switch statements do what the programmer tells them to do. They don't "judge" anything.

    The programmer has given this association between strengthCount and a String:
    1 => Very Weak
    ...
    5 => Strong

    Change that association if you want the code to do something different.
    The programmer has not put code in the switch statement for the String: "Very Strong" so it will never be used.


    Print out the value of strengthCount just before the switch statement so you know what value has been computed.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    How is the switch statement more efficient than if/else?

    It sounds like the result is one greater than you'd like, so check your logic. Can strengthCount ever be zero?

  12. #12
    Junior Member
    Join Date
    Jun 2014
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Because it uses less lines of code. And yes it can be zero, if no password exists then it is indeed zero. When a password which is an empty string is checked then the program still returns "no password" even with the case statement, so i cant understand why everything else is messing up. Please, i have had alook at the problem and cannot find a solution, im getting very frustrated with it and i know that's a big part of programming but i posted this thread for a solution, so please help!

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Because it uses less lines of code.
    That has very little to do with efficiency.

    Look at the switch statement you've coded. Using a piece of paper write down the Strings that are set for the values 0 to 5 and see if they are what you want to happen.
    0=> ??
    through
    5=> ??
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Jun 2014
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Well according to the lessons that i have taken it is better to code with less lines. Iv already done this and they are not, i think it is a syntax error but i am unsure as i am unfamiliar with Java. If the general solution works fine with IF statements then why does the case statement not work! This is what i do not understand.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Look at the switch statement you've coded. Using a piece of paper write down the Strings that are set for the values 0 to 5 and see if they are what you want to happen.
    0=> ??
    through
    5=> ??
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Jun 2014
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    I can actually check the output perfectly fine, i have a Binary Tree which allows me to search users and test this. I know which passwords equal what strengths! Although i am only showing this method in my post i have created the rest of the program! I have posted a screenshot of this below.

    passstr.jpg

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Please copy all text and paste it here. No images. It's not possible to copy text from images when responding.

    the output perfectly fine
    Does that mean you have solved the problem with the switch statement?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Jun 2014
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    1 Shaun Password1! memorable1
    2 Andrew Password2 memorable2
    3 Danny Password memorable3
    4 Zac password memorable4
    5 Lauren pass memorable5
    6 Jake memorable6
    7 Andy p7 memorable7
    8 Chris PasswordŁ8 memorable8
    9 Theo p!9 memorable9
    10 Matthew password10 memorable10

    USER FOUND
    Search Results:

    ID: 1
    Name: Shaun
    Password: Password1!
    Password Strength: Strong
    Enycrpted Password: !1drowssaP
    Memorable Word: memorable1

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    I don't know what that is for.

    the output perfectly fine
    Does that mean you have solved the problem with the switch statement?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Jun 2014
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    No, because the output that the switch statment has been producing has been incorrect, whereas the IF statements was producing the correct results, which is why i cant figure out why it is not working

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Are you going to work on the switch statement problem? If so please respond to my question.
    Otherwise mark the thread as solved.

    Look at the switch statement you've coded. Using a piece of paper write down the Strings that are set in the code for the values 0 to 5 and see if they are what you want to happen.
    0=> ??
    through
    5=> ??
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    blobby404 (June 19th, 2014)

  23. #22
    Junior Member
    Join Date
    Jun 2014
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Iv found the error. I had repeated some strings in the switch statement multiple times. So actually output was correct, i had just repeated some strings by accident.

    OLD VERSION OF CODE:

     
          //StrengthCount Converted to string - CASE STATEMENT
     
                switch (strengthCount){
                case 0:
                strengthWord = "No password; da fuq hacks";
                 break;
                case 1:
                strengthWord = "Very Weak";
                break;
                case 2:
                strengthWord = "Very Weak";
                break;
                case 3:
                strengthWord = "Weak";
                break;
                case 4:
                strengthWord = "Medium";
                break;
                case 5:
                strengthWord = "Strong";
                break;
            }

    NEW VERSION OF CODE:

     
     switch (strengthCount){
                case 0:
                strengthWord = "No password; da fuq hacks";
                 break;
                case 1:
                strengthWord = "Very Weak";
                break;
                case 2:
                strengthWord = "Weak";
                break;
                case 3:
                strengthWord = "Medium";
                break;
                case 4:
                strengthWord = "Strong";
                break;
                case 5:
                strengthWord = "Very Strong";
                break;
            }

  24. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Glad you found it.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #24
    Junior Member
    Join Date
    Jun 2014
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to turn my If statement into a case/switch statement?

    Thank you. Thank you to you and everyone else who gave advice.

Similar Threads

  1. Using variables in switch statement case clauses
    By kunimaro15689 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 19th, 2014, 06:21 PM
  2. using case switch statement. display salary and bonus.. help me
    By yarnue14 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 11th, 2013, 08:54 PM
  3. [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
  4. Replies: 9
    Last Post: February 24th, 2013, 06:51 PM
  5. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM

Tags for this Thread